in reply to HoH Sorting by value

Maybe I wasn't specific enough in my original question. Given the data above, I would like to print each ss_number, employee_number, & years_employed only sorted by years_employed. In other words, I would like this, only sorted:

foreach my $ss_number {keys %my_hash} { print "$my_hash{$ss_number}{$employee_number}\n"; }

Replies are listed 'Best First'.
Re: Re: HoH Sorting by value
by Thelonius (Priest) on Oct 23, 2002 at 18:22 UTC
    Your data structure doesn't look right to me, unless you really want to have multiple employee_numbers per ss_number.

    Probably you want a hash with ss_number as the key and the value as either an array ref or hash ref with all the values associated with that ss_number. E.g.

    $myhash{'555236801'} = [3035, 2]; or $myhash{'555236801'} = { empno => 3035, years => 2 };
    Anyway, one easy way to get what you want is like this (at least on Unix (using the latter data structure:
    open SORTEDOUT, "|sort +2" or die "cannot open sort: $!\n"; for (keys %myhash) { print SORTEDOUT $_, " ", $myhash{$_}{empno}, " ", $myhash{$_}{year +s}, "\n"; } close (SORTEDOUT) or die "Error on sort: $!\n";