erselbst
Goto Top

Verschachtelte Hash mit Array

Hallo!
Ich möchte eine Navigation ausgeben, wo zuerst die Kategorien sortiert ausgegeben werden, und zu jeder Kategorie die Artikel sortiert ausgegeben werden.

Mein Aktuelles Problem ist, wie bekomme ich die Artikel als Array in die jeweilige Kategorie?

#!/usr/bin/perl

BEGIN {
%nav = ();
}

while (<DATA>) {
  chomp($_);
  my ($cat, $post) = split(/\:/, $_);
  $nav{$cat} = push(@{$nav{$cat}}, $post);
}

END {

foreach my $category(sort keys %nav) {
  print $category,"\n";  
  foreach my $article (sort {lc($a) cmp lc($b)} @{$nav{$category}} ) {
    print "- ",$article,"\n";  
  }
}

}
__DATA__
cat1:post1
cat1:post2
cat1:post3
cat2:post1
cat2:post2
cat3:post1
cat3:post2
cat3:post3

Content-Key: 72270692150

Url: https://administrator.de/contentid/72270692150

Printed on: April 28, 2024 at 01:04 o'clock

Mitglied: 7907292512
Solution 7907292512 Sep 13, 2023 updated at 17:29:41 (UTC)
Goto Top
Aus dem doppelt gemoppelten
$nav{$cat} = push(@{$nav{$cat}}, $post);
werde
push(@{$nav{$cat}}, $post);

TIO.run

Fertsch.
Member: erselbst
erselbst Sep 14, 2023 at 06:20:20 (UTC)
Goto Top
Hehe, doppelt gemoppelt ;) Eigentlich hatte ich schon die Daten und weise diese nochmal dem Hash zu.

Vielen Dank für deine Hilfe!