$ cat pm_1198871_a.pl use strict; use warnings; # Parallel arrays: my @first_name = ('Joe', 'Bob', 'Mary', 'Sue'); my @last_name = ('Smith', 'Jones', 'Blige', 'Parker'); my @age = (25, 43, 19, 57); # An array of hashes: my @people = ( { first=>'Joe', last=>'Smith', age=>25 }, { first=>'Bob', last=>'Jones', age=>43 }, { first=>'Mary', last=>'Blige', age=>19 }, { first=>'Sue', last=>'Parker', age=>57 }, ); # Parallel arrays: Make a sort-by-age list of indices: my @indices = sort { $age[$a] <=> $age[$b] } 0 .. $#first_name; print "Using parallel arrays, sorted by age\n"; for my $i (@indices) { print "$first_name[$i] $last_name[$i] $age[$i]\n"; } # Array of hashes: Make a sort-by-age list of indices: @indices = sort { $people[$a]{age} <=> $people[$b]{age} } 0 .. $#people; print "\nUsing a hash, sorted by age\n"; for my $i (@indices) { print "$people[$i]{first} $people[$i]{last} $people[$i]{age}\n"; } $ perl pm_1198871_a.pl Using parallel arrays, sorted by age Mary Blige 19 Joe Smith 25 Bob Jones 43 Sue Parker 57 Using a hash, sorted by age Mary Blige 19 Joe Smith 25 Bob Jones 43 Sue Parker 57 #### # Parallel arrays: Sort our data by age my @indices = sort { $age[$a] <=> $age[$b] } 0 .. $#first_name; @first_name = @first_name[@indices]; @last_name = @last_name[@indices]; @age = @age[@indices]; print "Using parallel arrays, sorted by age\n"; for my $i (0 .. $#first_name) { print "$first_name[$i] $last_name[$i] $age[$i]\n"; } #### # Array of hashes: Sort our data by age @people = sort { $a->{age} <=> $b->{age} } @people; print "\nUsing a hash, sorted by age\n"; for my $hr (@people) { print "$hr->{first} $hr->{last} $hr->{age}\n"; } #### my $ma = { first=>'Morticia', last=>'Addams', age=>undef, favorite_color=>'black', address=>{ street_num=>131313, street_name=>'Mockingbird Lane', city=>'Perish', st=>'NY', zip=>13131 }, }; my $white_house = { class=>'GOVT', branch=>'Executive', usage=>'Presidents Residence', ... address=>{ street_num=>1600, street_name=>'Pennsylvania Avenue, N.W.', city=>'Washington', st=>'DC', zip=>20500 }, }; print_address($ma->{address}); print_address($white_house->{address}); sub print_address { my $addr = shift; print "$addr->{street_num} $addr->{street_name}\n$addr->{city} $addr->{st} $addr->{zip}\n"; }