Antwort: Re: Sortieren mit Perl...

Kai.Windmoeller at rokd.de Kai.Windmoeller at rokd.de
Wed Jan 26 13:58:57 CET 2000


> Dann sortieren bzw sortiert ausgeben ...
>
> foreach $user ( sort keys %hash ) {
>     foreach $col1 ( sort keys ${ $hash{$user} } ) {
>         foreach $col2 ( sort keys ${ $hash{$user}{$col1} } ) {
>             foreach $col3 ( sort keys ${ $hash{$user}{$col1}{$col2} } ) {
>         print "$user;$col1;$col2;$col3\n";
>             }
>         }
>     }
> }

Dies sortiert, meines wissens nach,

primär nach $user,
dann nach $col1,
dann nach $col2,
und dann nach $col3

und ist damit am Ziel vorbei.

Perl kann mit rationalen zahlen umgehen und sortieren,
weil alle zahlen ersteinmal in perl als rationale
gespeichert werden (siehe doku nahe 'use integer')

Das oben funktioniert, wenn mann das Hash anders aufbaut



foreach (<STDIN>) {
     my @f=split(/;/,$_);
     $hash{$f[3]}{$f[1]}{$f[0]}{$f[2]}=1;
}

foreach $col3 ( reverse sort keys %hash ) {
    foreach $col1 ( reverse sort keys ${ $hash{$col3} } ) {
        foreach $user ( sort keys ${ $hash{$col3}{$col1} } ) {
            foreach $col2 ( sort keys ${ $hash{$col3}{$col1}{$user} } ) {
               print "$user;$col1;$col2;$col3\n";
            }
        }
    }
}

Gruß Kai





More information about the Linux mailing list