in reply to Help with Sorting
I fail to see any benefit in the structure of your data structure. If you have keys with names that contain consecutive intergers, then that is a big red flag saying you might need an array instead. Try this instead:
Now you have a simpler data structure that should provide the same functionality. If you need the value for key 'cust9' then you use index 8 instead:use Data::Dumper; my $customers = [ map { { current => [ $_ ] } } (13, 110, 10, 102, 108, 130, 10, 107, 5, 210, 1, 160, 16) ]; print Dumper $customers;
Sorting complex datastructures is always a little tricky, but we have a more simplified datastructure to work with now:print $customer->[8]->{current}->[0];
Be sure to use Data::Dumper often and liberally to observe what you datastructure really looks like, and use an array when you have consecutive indices.my @sorted = sort { $a->{current}->[0] <=> $b->{current}->[0] } @$customer; print Dumper \@sorted;
UPDATE: yes, i read that. I really do wish that you would have stated that 'cust1' was just a bogus key name. However, i still stand by my recommendation, unless you are doing more lookups than sorts. Leaving out code is fine, but don't leave out the details, please. "The actual data structure is much larger than it appears below but the following code is sufficient to describe the issue" in no way implies that "The keys of the top level hash are our actual customer names which I did not feel should be listed in this forum."
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Help with Sorting
by ChrisR (Hermit) on Jul 13, 2004 at 19:32 UTC |