peterlein: Sortierung

Hallo zusammen!

Ich habe ein kleines Skript, in dem bestimmten usern bestimmte Zahlen zugeordnet werden:
....
while (my ($user, $anzahl) = each %anzahl) {
 print "<tr>\n<td>$user</td>\n<td>$anzahl</td>\n</tr>\n";
}
.....

Jetzt möchte ich alles nach username alphabetisch sortiert haben. Setzte ich sort vor each %anzahl, so bekomme ich eine Sortierung nach $anzahl. Wie funktioniert es richtig?

peterlein

  1. Hallo,

    while (my ($user, $anzahl) = each %anzahl) {
    print "<tr>\n<td>$user</td>\n<td>$anzahl</td>\n</tr>\n";
    }
    .....

    Jetzt möchte ich alles nach username alphabetisch sortiert haben. Setzte ich sort vor each %anzahl, so bekomme ich eine Sortierung nach $anzahl. Wie funktioniert es richtig?

    Ich bevorzuge für solche Fälle die Funktion keys.
    foreach $key (sort keys %hash)
      {
      print "$key = $hash{$key}\n";
      }

    Grüße
      Klaus