in reply to assigning regex matches to variables

Hope I haven't double-posted this... Problem solved -- thank you all VERY MUCH for your help. Having two errors at the same time made it hard to figure out. I didn't know that adding parentheses would force the statement to be evaluated in list context. I'm surprised that the distinction between list and scalar context isn't emphasized more in any of the regex documents I have read and I don't recall seeing the trick of forcing list context by using parentheses. However, it must be a common thing to want to change the value of a variable using a regex -- is there some other common syntax for that?

  • Comment on Re: assigning regex matches to variables

Replies are listed 'Best First'.
Re^2: assigning regex matches to variables
by jethro (Monsignor) on May 28, 2011 at 21:23 UTC

    context plays such a central role in perl that it isn't repeated at every opportunity how you can get it. Usually every tutorial or reference text on perl will tell you about context in one of the first chapters. Note that CountZero's exerpt of the regex documentation specifically mentions context

    Changing the value of a variable with a regex is done with the s/// operator, i.e. for example

    $f=~ s/must/can/g;

    would subsitute every occurance of "must" in the variable $f to "can"