hkates has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Create a hash for each unique captured regex variable
by Athanasius (Archbishop) on Jan 29, 2015 at 03:11 UTC | |
|
Re: Create a hash for each unique captured regex variable
by CountZero (Bishop) on Jan 29, 2015 at 06:57 UTC | |
|
Re: Create a hash for each unique captured regex variable
by NetWallah (Canon) on Jan 29, 2015 at 01:13 UTC | |
|
Re: Create a hash for each unique captured regex variable
by Anonymous Monk on Jan 29, 2015 at 00:24 UTC | |
|
Re: Create a hash for each unique captured regex variable
by MidLifeXis (Monsignor) on Jan 29, 2015 at 13:39 UTC |