substitution variable won't change in regexp 
Author Message
 substitution variable won't change in regexp

I just know I'm going to feel stupid when this is explained to me.

---



    s/a/$a/;
    print "Should be: $a Was: $_\n";

Quote:
}

---
                  Output
Expected by me      |  what I got instead
                    |
Should be: z Was: z |  Should be: z Was: z
Should be: y Was: y |  Should be: z Was: z
Should be: x Was: x |  Should be: z Was: z
Should be: w Was: w |  Should be: z Was: z

I would expect this if I used the "o" qualifier as in 's/a/$a/o',
but I don't understand why the $a in the regular expression does
not get changed on each loop interation if I don't use the "o".

Thanks in advance for any help,

Larry



Mon, 29 Jun 1998 03:00:00 GMT  
 substitution variable won't change in regexp
: I just know I'm going to feel stupid when this is explained to me.

: ---

: $_ = 'a';

:  

:     s/a/$a/;
:     print "Should be: $a Was: $_\n";
: }

: ---
:                   Output
: Expected by me      |  what I got instead
:                     |
: Should be: z Was: z |  Should be: z Was: z
: Should be: y Was: y |  Should be: z Was: z
: Should be: x Was: x |  Should be: z Was: z
: Should be: w Was: w |  Should be: z Was: z

: I would expect this if I used the "o" qualifier as in 's/a/$a/o',
: but I don't understand why the $a in the regular expression does
: not get changed on each loop interation if I don't use the "o".

: Thanks in advance for any help,

I entered your program as follows and got different, but understandable
results (Please forgive my indentation style):

8x------snip------x8

#!/usr/local/bin/perl5

use strict;

require 5.001;


my($a);

$_ = 'a';


   {
   s/a/$a/;
   print "Should be: $a Was: $_\n";
   }

8x------snip------x8

Output:

Should be: z Was: z
Should be: y Was: z
Should be: x Was: z
Should be: w Was: z

This is the way I see it:
Each time through the loop the substitution changes the first occurrence of
the letter 'a' to whatever the variable contains.  So we should have:

Value of $a  Initial value of $_   New value of $_
    z                 a                  z
    y                 z                  z
    x                 z                  z
    w                 z                  z

This all makes sense to me.  What I do not understand is why my output does
not match yours.

: Larry

Hope this helps,
Jim Shapiro



Mon, 29 Jun 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. regexp with variable substitution values

2. Variable substitution into a regexp

3. Substitution won't match for me

4. regexp: saving backreferences that won't overwrite existing values

5. Regexp containing plus-chars won't work?!?!?

6. Script won't write to file or change directory

7. changing $ 'variables' in regexprs

8. system() method won't change directories!

9. Button image won't change to text

10. Changing to Perl regexp's?

11. variable substitution in pattern for `s'

12. Substitution with parameters and variable don't work s/$search/$replace/i

 

 
Powered by phpBB® Forum Software