This is a simplified version of my problem:
#!/usr/bin/perl $line="One two three four five"; if($line =~ m/(one)\s(two)\s(three)\s(four)\s(five)/i){ print "1: $1\n"; print "2: $2\n"; print "3: $3\n"; print "4: $4\n"; print "5: $5\n"; }#if
produces the expected:
[me]$>perl -w regexp-test2.pl 1: One 2: two 3: three 4: four 5: five
BUT if you try to do anything to one of the returned values such as this:
#!/usr/bin/perl $line="One two three four five"; if($line =~ m/(one)\s(two)\s(three)\s(four)\s(five)/i){ $one=$1; $one =~ s/one/ONE/i; print "1: $one\n"; print "2: $2\n"; print "3: $3\n"; print "4: $4\n"; print "5: $5\n"; }#if
you loose the other returned values:
[me]$>perl -w regexp-test3.pl 1: ONE Use of uninitialized value $2 in concatenation (.) or string at regexp +-test2.pl line 12. 2: Use of uninitialized value $3 in concatenation (.) or string at regexp +-test2.pl line 13. 3: Use of uninitialized value $4 in concatenation (.) or string at regexp +-test2.pl line 14. 4: Use of uninitialized value $5 in concatenation (.) or string at regexp +-test2.pl line 15. 5:
Can any one explain to me why this happens?
As far as my program is concerned waiting until all the returned values are assigned and then processing things works but I do not understand why doing something to a variable that has been assigned a value from $1 results in loosing the other returned values.
In reply to Regexp and substitution question by bdalzell
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |