Tmms has asked for the wisdom of the Perl Monks concerning the following question:
Hello
I am fairly new at perl and I am struggling with references. I will post fictitious example underneath of what I would like to do. Hopefully someone can help me.
Intput: textfile with lines likes this:
Obi Wan "tab" Jedi "newline"
Yoda "tab" Jedi "newline"
Count Dooku "tab" Sith "newline"
...
Desired output:
A hash where the affiliations (Jedi of Sith) are the keys and the values are arrays of names (Obi Wan, Yoda, ...)
I want to create the hash while "iterating" over the file line by line.
Problem:
I got stuck with the construction of the hash.
First I checked if the key (affiliation) was defined. If not, I added the appropriate key-value pair ($hash{affiliation} => [name])
But I could not find a way to add a name to the anonymous array when the affiliation was already a key.
Many thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Making a hash of arrays using references
by kcott (Archbishop) on Apr 09, 2017 at 07:07 UTC | |
|
Re: Making a hash of arrays using references
by NetWallah (Canon) on Apr 09, 2017 at 06:23 UTC | |
|
Re: Making a hash of arrays using references (push)
by LanX (Saint) on Apr 08, 2017 at 22:10 UTC | |
by Tmms (Initiate) on Apr 14, 2017 at 19:59 UTC |