ExReg has asked for the wisdom of the Perl Monks concerning the following question:
As a follow up to 1161099, I have an array of stuff that I have extracted from a big file via the use of regexes, call it @excerpts. Each array entry is then processed with regexes to get additional pieces of information. I would like to store those pieces of information in that same array, making a sort of array of hashes.
In the example I gave, my file contents are
$fc = 'abcdfoofrobnicatebardefforspambazghi';I get my @excerpts array filled with
$re2 = qr/(fo.)(.*?)(ba.)/; push @excerpts, $1 while $fc =~ /($re2)/g;
I now have the @excerpts array
0:foofrobnicatebar 1:forspambaz
I now use the same $re2 regex to get
0:$1='foo' $3='bar' 1:$1='for' $3='baz'
I would like to add these to the array, and make a sort of array of hashes. I would like to add {fpart} for $1 and {bpart} for $3 to each entry in @excerpts. The problem I have is that the only way I have found to do that is so ugly that I am afraid I will have bad Perl nightmares this weekend. The only way I have found to add or read the hash values is
$%{$excerpts[$i]}{fpart} = $1; $%{$excerpts[$i]}{bpart} = $2;
They can be read back with the exact same expression. This construct just looks so unnatural that I am afraid it will invoke some unnatural activity from my PC when I have my back turned. None of the normal AoH notation worked in this case. Is there a better expression?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Adding hashes to already existing array
by Marshall (Canon) on May 06, 2016 at 20:00 UTC | |
by ExReg (Priest) on May 06, 2016 at 20:32 UTC | |
|
Re: Adding hashes to already existing array
by stevieb (Canon) on May 06, 2016 at 17:50 UTC | |
by ExReg (Priest) on May 06, 2016 at 18:50 UTC | |
by haukex (Archbishop) on May 06, 2016 at 19:35 UTC | |
by ExReg (Priest) on May 06, 2016 at 20:22 UTC | |
by poj (Abbot) on May 06, 2016 at 20:26 UTC | |
by haukex (Archbishop) on May 06, 2016 at 20:32 UTC | |
| |
by ExReg (Priest) on May 11, 2016 at 14:11 UTC | |
by haukex (Archbishop) on May 12, 2016 at 10:03 UTC |