in reply to Re^2: CSV regex with hash/array program plan
in thread CSV regex with hash/array program plan

when ((s/(\b[A-E]{3}\b)/XXX/g)) {@array[i,1] = $1}

Capture groups don't work the same way in  s/// substitution versus  m// matching:

c:\@Work\Perl>perl -wMstrict -le "my $s = 'xxx aBc yyy dE zzz'; print qq{\$s: '$s'}; ;; print qq{\$1: '$1'} if $s =~ s{ [AaBbCcDdEe]+ }{XXX}xmsg; print qq{\$s: '$s'}; " $s: 'xxx aBc yyy dE zzz' Use of uninitialized value $1 in concatenation (.) or string at -e lin +e 1. $1: '' $s: 'xxx XXX yyy XXX zzz'
Capture groups work in a potentially surprising way in  s/// substitution and  m// matching:
c:\@Work\Perl>perl -wMstrict -le "my $s = 'xxx aBc yyy dE zzz'; print qq{\$s: '$s'}; ;; print qq{\$1: '$1'} if $s =~ s{ ([AaBbCcDdEe]+) }{XXX}xmsg; print qq{\$s: '$s'}; " $s: 'xxx aBc yyy dE zzz' $1: 'dE' $s: 'xxx XXX yyy XXX zzz'
(note that only the last group matched is captured). Please see perlre, perlrequick, and perlretut.
Also: I don't think  @array[i,1] = $1 is going to work the way you think it will whatever the value of  $1 may be (but I'm not sure just what you expect from this expression). Please see Slices in perldata. (Update: Something like this works for hashes: see  $; in perlvar. There's a more complete discussion of this old trick somewhere, but I can't locate it right now — anyone know where it is? (Update: Anonymonk informs me this is Multi-dimensional array emulation in perldata. This section was apparently added with Perl version 5.16.0 or 5.16.1. I only had 5.14 available locally and so missed it.))

Replies are listed 'Best First'.
Re^4: CSV regex with hash/array program plan
by Anonymous Monk on Nov 24, 2014 at 01:16 UTC
    Something like this works for hashes: see $; in perlvar. There's a more complete discussion of this old trick somewhere, but I can't locate it right now — anyone know where it is?
    It's in perldata... 'Multi-dimensional array emulation'.