Thread Hash mit Hash befüllen in For-Schleife nimmt nur das letzte Element (3 answers)
Opened by cbxk1xg at 2010-11-25 15:59

cbxk1xg
 2010-11-25 15:59
#143038 #143038
User since
2003-10-20
496 Artikel
BenutzerIn
[default_avatar]
Hallo, ich stehe grade auf dem Schlauch. Ich will in einer Schleife einen Hash mit einem anderen Hash befüllen. Es wird aber nur das letzte Element in den Ziel-Hash gepackt.

Das ist nur ein Auszug aus meinem Skript. Ganz oben ist use strict, use warning und taint mode an!!!

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if ($FORM{'type'} eq "db") {
my %UDIoutput = ();
my $count = 0;

        for my $line (@DATABASE) {
        $count++;
        my ($type,$datestamp,$file,$title) = split(/\e/, $line);
        $file =~ s/\//,/g;
        my $PIDurl = $HOSTurl."?PID=".$file;
        my $JSONurl = $HOSTurl."?PID=".$file."&udi=json&type=pid";

        my %entry = ( 'datestamp' => $datestamp, 'pid' => $PIDurl, 'json' => $JSONurl);

        %UDIoutput = ( $count => \%entry );
        }

# create and sort the JSON ouput
use JSON::PP;
my $json_text = JSON::PP->new()->sort_by(sub { $JSON::PP::a cmp $JSON::PP::b })->pretty(1)->encode(\%UDIoutput);
print "Content-type: text/html\n\n";
print $json_text;
}


Das Ergebnis ist:
Code: (dl )
1
2
3
4
5
6
7
{
"43" : {
"datestamp" : "201011241811",
"json" : "http://foo.bar/?PID=static,ressort,Internet,test_de&udi=json&type=pid",
"pid" : "http://foo.bar/?PID=static,ressort,Internet,test_de"
}
}

View full thread Hash mit Hash befüllen in For-Schleife nimmt nur das letzte Element