in reply to Re^4: Adding hashes to already existing array
in thread Adding hashes to already existing array

Hi ExReg,

if I first get the content into $value before overwriting it

$excerpts[$i] = { fpart=>$1, bpart=>$3, content=>$excerpts[$i] }; will do what you want.

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^6: Adding hashes to already existing array
by ExReg (Priest) on May 06, 2016 at 20:35 UTC

    Exactly what I wanted

      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.

        I have been bitten by that enough times. In this case, since the array was created by using /($re2)/, would it be necessary to test against $re2 in this case?