Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: HoH Sorting by value

by Anonymous Monk
on Oct 23, 2002 at 17:43 UTC ( [id://207460]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://207460]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-12-08 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.