in reply to HoH Sorting by value

This should give you a sorted list of keys for your HoHoH
my @sorted = sort { $hash{$a}->{employee_number}->{years_employed} <=> $hash{$b}->{employee_number}->{years_employed} } keys %hash;
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: HoH Sorting by value
by Anonymous Monk on Oct 23, 2002 at 17:43 UTC
    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.

      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'

      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