in reply to Re: Adding hashes to already existing array
in thread Adding hashes to already existing array
$fc = 'abcdfoofrobnicatebardefforspambazghi'; $re2 = qr/(fo.)(.*?)(ba.)/; push @excerpts, $1 while $fc =~ /($re2)/g; print "0: $excerpts[0]; print "0: $excerpts[0]; for my $i ( 0 .. 1 ) { $excerpts[i] =~ /$re2/; $%{$excerpts[$i]}{fpart} = $1; $%{$excerpts[$i]}{bpart} = $3; } print "0{fpart}: $%{$excerpts[0]}{fpart}\n; print "0{bpart}: $%{$excerpts[0]}{bpart}\n; print "1{fpart}: $%{$excerpts[1]}{fpart}\n; print "1{bpart}: $%{$excerpts[1]}{bpart}\n; __END__ 0:foofrobnicatebar 1:forspambaz 0{fpart}: foo 0{bpart}: bar 1{fpart}: for 1{bpart}: baz
Hope I typed all this correctly
Edit: changed $2 to $3 in for loop. Running it on PC with perl yields above results with exception of 1{bpart} for some reason
Addendum: I also realize that I could have started out with an array of hashes and put the original contents into $excerpts[$i]{contents} and then from it gotten $excerpts[$i]{bpart} $excerpts[$i]{fpart} with the normal AoH syntax, but this question is for an already existing array with stuff in $excerpts[$i].
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Adding hashes to already existing array
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 06, 2016 at 20:35 UTC | |
by stevieb (Canon) on May 06, 2016 at 20:50 UTC | |
| |
by ExReg (Priest) on May 11, 2016 at 14:11 UTC | |
by haukex (Archbishop) on May 12, 2016 at 10:03 UTC |