Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I know this is somewhat simple, but I've been unable to wrap my head around it. I have a large txt file in the format:

*******************Folder North*************** Folder : North Folder : Lab Host : host42016 VM : host42229 VM : host42235 VM : host42236 VM : host42237 VM : host42238 VM : host42239 VM : host42240 VM : host42241 VM : host42242 VM : host42252 Host : host42049 VM : host42361 VM : host42362 VM : host42363 VM : host42364 VM : host42365 VM : host42366 VM : host42367 VM : host42368 VM : host42369 VM : host42370 *******************Folder South*************** Folder : South Folder : Staging Host : host42124 VM : host42248 VM : host42249 VM : host42250 VM : host42230 VM : host42251 VM : host42243 VM : host42244 VM : host42245 VM : host42246 VM : host42247 Host : host42125 VM : host42299 VM : host42300 VM : host42301 VM : host42302 VM : host42303 VM : host42304 VM : host42305 VM : host42306 VM : host42307 VM : host42308

I want to read the file, and populate a hash with the "Host" as the key the various "VM" as it's values. Seems easy enough and I have that with:

open (FILE, 'data.txt') || die "Unable to open file"; while (<FILE>) { $line=$_; if ($line =~ /Folder/ || $line eq ""){ next; } ($type,$name)=split(/:/); $type =~ s/^\s+//; $type =~ s/\s+$//; $name =~ s/^\s+//; $name =~ s/\s+$//; if ($type=~"Host"){ $host=$name; }elsif ($type=~"VM"){ $guest=$name; push @{$hash{$host}}, $guest; } }

Where I'm running into a brick wall is:

  1. how do I remove duplicates (Host and VM)?
  2. how do I then count the # of values per key?
I've googled, RTFM, etc; but nothing I've tried has worked to my satisfaction.


In reply to Parsing text file into a hash with multiple values per key by dayton

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (10)
    As of 2015-07-02 10:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (33 votes), past polls