in reply to Data Structures and Sorting

Hi there,
I had a pop at this, and ended up with a solution that's a bit too much like line noise. Anyway, here you go:
#!/usr/bin/perl use warnings; use strict; #Your data, I messed with the Group attribute to test my code. my $hoh = { '1018884184639' => { 'Group' => 'Marketing', 'System Name' => '1018884184639', 'Authorization' => 'mrossa' }, '1017939213033' => { 'Group' => 'Marketing', 'System Name' => '1017939213033', 'Last Update' => 'Thu Apr 18 14:29:30 2002', 'Support Tools' => 'EGstools', 'Source Code Management' => 'EGscm', 'Production Platforms' => 'EGpp', 'Design Tools' => 'EGdest', 'Authorization' => 'dsmith', 'Test Tools' => 'EGtt', 'Database' => 'EGdb', 'URL' => 'http://www.perlmonks.com', 'Middleware' => 'EGmidd', 'Database Tools' => 'EGdb', 'Found Internal Libraries' => 'EGfdxi', 'Development Languages' => 'EGdl', 'Development Tools' => 'EGdtools', 'Fail Over Software' => 'EGfos' }, '1017939028902' => { 'Group' => 'Shipping', 'System Name' => '1017939028902', 'Last Update' => 'Fri Mar 29 10:32:44 2002', 'Support Tools' => '9', 'Source Code Management' => '10', 'Production Platforms' => '1', 'Design Tools' => '12', 'Authorization' => 'ecartman', 'Test Tools' => '11', 'Database' => '4', 'URL' => 'http://www.osdncom', 'Middleware' => '2', 'Database Tools' => '5', 'Found Internal Libraries' => '7', 'Development Languages' => '6', 'Development Tools' => '8', 'Fail Over Software' => '3' }, }; #We have a hash of system hashes, and we want to sort/group by the key +s within #the system hashes. #First, find out what attributes you're going to sort/group by my %sorted_by_attribute; #Loop through all the systems foreach my $system (keys %$hoh) { #Loop through all the keys found within each system, eg 'Devel +opment Tools' foreach my $attribute (keys %{$hoh->{$system}}) { #Get the value of that key, eg '8', and push the syste +m name onto #an array which is unique to that attribute, then the +value of that attribute my $attribute_value = $hoh->{$system}{$attribute}; push @{$sorted_by_attribute{$attribute}{$attribute_val +ue}}, $system; #Offhand, $sorted_by_attribute{$attribute}{$attribute_ +value} is an array reference, #hence the deref'ing curlies and @ symbol. (You can't +put a straight array into #a hash) } } #Now %sorted_by_attributes looks like this: #$VAR1 = { # 'Group' => { # 'Marketing' => [ # '1017939213033', # '1018884184639' # ], # 'Shipping' => [ # '1017939028902' # ] # }, # . # . # . # } #Print the data foreach my $attribute (sort keys %sorted_by_attribute) { my $individual_values = $sorted_by_attribute{$attribute}; print $attribute, "\n"; foreach my $value (keys %$individual_values) { my @systems = @{$sorted_by_attribute{$attribute}{$valu +e}}; print $value, " := "; print join ", ", @systems; print "\n"; } print "\n"; }
Blech.
Hopefully the comments are enough to allow you to understand it
cheers

davis
Is this going out live?
No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
Update: Fixed missing octothorp for comment. D'oh.