Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found