Philipp Hasenfratz: ?....? bei Hash, wie push bei Liste ?

Beitrag lesen

Halihallo

for (my $i = 0; $i < 1; $i++)    {   #die erste Zeile einlesen
$headdata = <FILE>;

Na, was soll'n diese for-Schleife? - Die erste Zeile einer Datei liest man immer noch über $headdata = <FILE> ein, ohne die for-Schleife...

my $unixzeit = $1 if $headdata =~ /<unix>(\d+?)</unix>/;
         push @postingliste ,$unixzeit, $_;

$postinghash{$unixzeit} = $_;

my @sort = sort { $a <=> $b } keys %postinghash;

my @topzwens = splice(@sort, 0, 19);

foreach my $unixtime (@topzwens) {
   my $related_file = $postinghash{$unixtime};
}

ach ja, das setzt voraus, dass jede mögliche unixtime wirklich nur einmal vorkommt, sonst gehen gleiche unixtimes verloren. Falls du dies nicht ausschliessen kannst, musst du jedem Hash-Key ein anonymes Array zuweisen:

unless (ref($postinghash{$unixzeit}) eq 'ARRAY') {
   $postinghash{$unixzeit} = [];
}
push @{$postinghash{$unixzeit}}, $_;

...

Viele Grüsse

Philipp