in reply to Re^6: Adding hashes to already existing array
in thread Adding hashes to already existing array
You should always check to ensure you have a match. Either use while(), or:
for my $i (0.. $#excerpts){ if (excerpts[$i] =~ /$re2/({ $%{$excerpts[$i]}{fpart} = $1; $%{$excerpts[$i]}{bpart} = $3; } }
...or you may get undef warnings, or unexpected results that you'll be head-scratching at, because the numbered variables hold their values in a loop, until overwritten. Example:
perl -wE '@a=(1,2,3); for(@a){/(2)/; say $1}' Use of uninitialized value $1 in say at -e line 1. 2 2
In the third iteration, we don't match /2/ at all, yet $1 still has the value from the last time it was captured.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Adding hashes to already existing array
by ExReg (Priest) on May 06, 2016 at 21:40 UTC | |
by stevieb (Canon) on May 06, 2016 at 23:12 UTC | |
by stevieb (Canon) on May 06, 2016 at 23:19 UTC | |
by ExReg (Priest) on May 07, 2016 at 00:07 UTC |