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>
|
|---|