bdalzell has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regexp and substitution question
by AnomalousMonk (Archbishop) on Aug 28, 2010 at 03:36 UTC | |
|
Re: Regexp and substitution question
by ikegami (Patriarch) on Aug 28, 2010 at 03:41 UTC | |
by bdalzell (Sexton) on Aug 28, 2010 at 14:27 UTC | |
|
Re: Regexp and substitution question
by toolic (Bishop) on Aug 28, 2010 at 03:46 UTC | |
|
Re: Regexp and substitution question
by oko1 (Deacon) on Aug 28, 2010 at 15:40 UTC | |
by pemungkah (Priest) on Aug 29, 2010 at 00:27 UTC |