Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Building a data structure - %HoH, %HoA?

by Anonymous Monk
on Nov 02, 2012 at 07:52 UTC ( [id://1001930]=note: print w/replies, xml ) Need Help??


in reply to Building a data structure - %HoH, %HoA?

Could be done via both Hash of Hashes as well as Hash of Arrays

See if this works....

Input File

team_1, location_2, tool_1, 4 team_3, location_1, tool_3, 3 team_2, location_3, tool_3, 2 team_2, location_4, tool_2, 5 team_3, location_2, tool_5, 3 team_1, location_1, tool_3, 1 team_2, location_4, tool_5, 6 team_4, location_2, tool_5, 3 team_5, location_4, tool_4, 2 team_2, location_4, tool_5, 3 team_3, location_3, tool_4, 4 team_2, location_4, tool_5, 3 team_4, location_1, tool_1, 1 team_1, location_3, tool_5, 3 team_3, location_2, tool_5, 5

Code

use v5.14; use Data::Dumper; my %locHash; foreach(`cat dataFile`){ my ($locName, $toolName, $toolNum)=(split(/\,/,))[1..3]; $locHash{$locName}->{$toolName}=$toolNum; } print "Data::Dumper:\n"; print Dumper(\%locHash); say "<ToolLocations>"; foreach my $locName(sort keys %locHash){ say " <ToolLocation>"; say " <LocationName>",chomp($locName),"</LocationName>"; say " <ToolList>"; foreach my $toolDetail(sort keys %{$locHash{$locName}}){ say " <Tool>"; say " <ToolName>",chomp($toolDetail),"</ToolName>"; say " <ToolNum>",chomp($locHash{$locName}->{$toolDet +ail}),"</ToolNum>"; say " </Tool"; } say " </ToolList>"; say " </ToolLocation>"; } say "</ToolLocations>";

Output

Data::Dumper: $VAR1 = { ' location_1' => { ' tool_3' => ' 1 ', ' tool_1' => ' 1 ' }, ' location_3' => { ' tool_5' => ' 3 ', ' tool_3' => ' 2 ', ' tool_4' => ' 4 ' }, ' location_4' => { ' tool_5' => ' 3 ', ' tool_2' => ' 5 ', ' tool_4' => ' 2 ' }, ' location_2' => { ' tool_5' => ' 5 ', ' tool_1' => ' 4 ' } }; <ToolLocations> <ToolLocation> <LocationName>0</LocationName> <ToolList> <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool </ToolList> </ToolLocation> <ToolLocation> <LocationName>0</LocationName> <ToolList> <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool </ToolList> </ToolLocation> <ToolLocation> <LocationName>0</LocationName> <ToolList> <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool </ToolList> </ToolLocation> <ToolLocation> <LocationName>0</LocationName> <ToolList> <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool <Tool> <ToolName>0</ToolName> <ToolNum>1</ToolNum> </Tool </ToolList> </ToolLocation> </ToolLocations>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1001930]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found