[Tutor] Modify a string multiple times

Tom Tucker tktucker at gmail.com
Tue Sep 28 17:47:31 CEST 2004


Hello all!  I am struggling again with something minor.  I suspect I
am making this more complicable than what is required.  Ha!

I am trying to modify a simple string twice when it is being readin
from a file.   As you can see in the below Python example this code
does work, however the string name changes from line, to newstring,
and then to newstring2.  I want the string name to remain as "line" as
seen in the Perl example.   Make sense?  Any recommendations on how to
achieve this?


Python 
########
< extra lines removed>
line = 'python and perl are both programming languages.'
newstring = re.sub('python','Python',line)
newstring2 = re.sub('perl','Perl',newstring)
print newstring2


Perl
#########
$line = "python and perl are both programming languages";
$line  =~ s/python/Python/g;
$line  =~ s/perl/Perl/g; 
print "$line\n";

* Notice how the Perl variable $line remains the same.


END RESULT: Python and Perl are both programming Languages


Thanks,

Tom


More information about the Tutor mailing list