in reply to Re: HoH Sorting by value
in thread HoH Sorting by value

Is there any way to do this without creating a new array of the sorted values? I really do appreciate the help, but that wasn't what I was hoping for.

Replies are listed 'Best First'.
Re: Re: Re: HoH Sorting by value
by Chmrr (Vicar) on Oct 23, 2002 at 17:47 UTC

    Hashes themselves cannot be sorted -- the closest one can get is to sort either the keys or the values of the hash, then iterate through them. See perldoc -q "sort a hash"

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Re: Re: HoH Sorting by value
by rdfield (Priest) on Oct 24, 2002 at 08:50 UTC
    Take broquaint's answer, remove the assignment, add a loop and a print. Job done.

    Or do you just want the code:

    my %hash; while (<DATA>) { my @data = split; $hash{$data[0]}->{employee_number} = $data[1]; $hash{$data[0]}->{years_employed} = $data[2]; } foreach (sort { $hash{$a}->{years_employed} <=> $hash{$b}->{years_employed} } keys %hash) { print "$_ $hash{$_}->{employee_number} $hash{$_}->{years_employed}\ +n"; }; __DATA__ SS123 EMP123 3 SS234 EMP124 2 SS456 EMP125 1
    prints
    SS456 EMP125 1 SS234 EMP124 2 SS123 EMP123 3

    rdfield