#!c:\perl\bin\perl.exe -w # Nothing interesting here, just setup for the example. use strict; { local $/; $_=; } my %columns=%{eval $_}; # Story: # The arrayrefs in %columns represent an entire record. # So in the example below "John, Jay, Aurora" in an entire # record. We produce @si, which tells us the order in which # the records should actually be in. This sort # is actually a lot more complicated than this but the # net result is that @si contains a list of "row" numbers # in the proper order. # Don't mess with this, PerlMonks. my @si=sort { $columns{field1}->[$a] cmp $columns{field1}->[$b] } (0..@{$columns{field1}}-1); # The part I'm feeling lazy about is here. I want to # rebuild the structure so that %columns has the # arrayrefs arranged in the proper order. (Bill, Bob, # John, Sue; then Raye, Apple, Jay, Shell; then Clio, # Calumet, Aurora, Elgin) It works now, but isn't terribly # efficient since %columns is enormous in Real Life. # i.e. do this without a temp hash. :) Extra points # for cleverness. Keep it self-contained: no fair # monkeying with the sort above. You have @si and %columns # to play with. my %foo; foreach(@si) { for my $k (keys %columns) { push(@{$foo{$k}}, $columns{$k}->[$_]); } } %columns=%foo; undef %foo; __DATA__ { field1 => [ 'John','Sue','Bill','Bob'], field2 => [ 'Jay','Shell','Raye','Apple'], field3 => [ 'Aurora','Elgin','Clio','Calumet'], }