Here is my input:

foo_1-a foo_2-b foo_3-b foo_4-b bar_1-a bar_2-a bar_3-b bar_4-a bar_5-b

And my desired output:

foo 4 foo_1-a foo_2-b foo_3-b foo_4-b bar 5 bar_1-a bar_2-a bar_3-b bar_4-a bar_5_b

I wish that I had code to show you, but I don't know where to start and am thinking perl may not be the best thing for the job. I want to build a hash for each foo or bar, where foo/bar are the values and the hash keys are the full word. e.g.:

%hash_bar =() bar_1-a => bar bar_2-a => bar bar_3-b => bar bar_4-a => bar bar_5-b => bar %hash_foo =() foo_1-a => foo foo_2-b => foo foo_3-b => foo foo_4-b => foo

Then I want to print any value in the hash (since they are all the same) followed by the number of keys in the hash, followed by the keys for each hash

It's a stretch to call this pseudo code, but just to clarify my question:

open FH, "<file.txt"; while (<FH>) { if (/((\S+)_\S+-\S+)) { #for each unique $1; %hash_$1 =(); # populate hash with keys $2 and values $1 $hash_$1($2)=$1; } }

I'm not expecting anyone to do this for me, but any direction to the function needed for this would be much appreciated.

I know that I wouldn't be able to create the hashes in that if statement. I would need to create a hash of all the unique $1 first (so that I could use the exists function) and then for each key in that hash, read through the file again, creating a new hash for each key in the original hash.

But that seems very inelegant, and I didn't know how to even write the pseudo code. Am I just totally on the wrong track?

Thanks!


In reply to Create a hash for each unique captured regex variable by hkates

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.