Gossamer Forum
Home : General : Perl Programming :

Pattern Matching: Just for the Masters of The forum

Quote Reply
Pattern Matching: Just for the Masters of The forum
Hello all. I posted a message about pattern matching these days...

I want to change the values of "a" and "b" on the string below to "alabama" and "boston".

Jerry Latimer, on Perl cgi-list of Perl.com sugested to do this little simple code as follow:

$str = "(a**2) - (b**2) + 4*a*b";
$str =~ s/a/alabama/;
$str =~ s/b/boston/;

so, i would obtain: $str = "alabama**2)-(boston**2) + 4*alabama*boston, but i obtain

$str = "alabama**2)-(boston**2) + 4*a*b

The problem is code is just changing the fist "a" to "alabama", not all a's of the string. Can anyone help me? Thanks a lot.. Again!
Quote Reply
Re: Pattern Matching: Just for the Masters of The forum In reply to
Not into reading the responses to your previous posts?

http://www.gossamer-threads.com/...um8/HTML/000986.html
Code:
$_ = '(a**2) - (b**2) + 4*a*b';
%names = (
a => 'alabama',
b => 'boston'
);
s/(a|b)/$names{$1}/g;

The code that was given to you by Jerry Latimer is flawed in 2 aspects.
1) When converting the b's to boston it will convert the b in the word alabama as well.
2) He isn't using the 'g' switch to convert all instances.

-- Gordon

------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);