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>

In reply to Re: Building a data structure - %HoH, %HoA? by Anonymous Monk
in thread Building a data structure - %HoH, %HoA? by Roboz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.