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

Exactly what I wanted

  • Comment on Re^6: Adding hashes to already existing array

Replies are listed 'Best First'.
Re^7: Adding hashes to already existing array
by stevieb (Canon) on May 06, 2016 at 20:50 UTC

    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?

        Always. Always check to ensure you have a match before you assign/use a numbered special var. If you're doing $something = $1, ensure $1 has what you want first.

        Test what you've been shown on this thread, then throw in some unexpected data that'll match a success, but'll partially match a failure.

        You stated you've been bitten before, so you already know the answer to your own question.

        I'd like to see the code you end up with. There's been a lot of scratch, so once you're done and have things working, reply to this thread with your code. We're not a review service (typically), but in this case, I'm curious.