|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Well like the subject says, i'm stumped trying to sort an array of hashes.
I've got an array full of references to hashes, and I can't figure out how to sort it. Any suggestions? |
|
#2
|
|||
|
|||
|
I am a beginner at perl but maybe this helps
print join ('', sort @hashes); i cant garanty it works but i juse it in one of my scripts to print so edit it and maybe it work |
|
#3
|
|||
|
|||
|
Well, you could just sort the array based on the referenced hashes (memory addresses), but that's probably not what you want.
What you have to do is write your own sort routine. For example: @Info = ( { fname => "John", lname => "Doe", age => 34 }, { fname => "Jane", lname => "Dae", age => 32 }, { fname => "Michael", lname => "Richards", age => 22} ); # Print out each persons information, sort it by their last name foreach $user_hash ( sort { $a->{lname} cmp $b->{lname} } @Info) { print "Name: $user_hash->{fname} $user_hash->{lname}\n"; print "Age: $user_hash->{age}\n"; } Look up the documents on sort if this doesn't make sense. Hope this helps. - Amir |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Sorting Arrays of Hashes?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|