sobering observation, python vs. perl

Rustom Mody rustompmody at gmail.com
Thu Mar 17 13:59:34 EDT 2016


On Thursday, March 17, 2016 at 11:24:00 PM UTC+5:30, BartC wrote:
> On 17/03/2016 17:25, Charles T. Smith wrote:
> > On Thu, 17 Mar 2016 19:08:58 +0200, Marko Rauhamaa wrote:
> 
> >>     my $str = "I have a dream";
> >>     my $find = "have";
> >>     my $replace = "had";
> >>     $find = quotemeta $find; # escape regex metachars if present
> >>     $str =~ s/$find/$replace/g;
> >>     print $str;
> >>
> >> with Python:
> >>
> >>     print("I have a dream".replace("have", "had"))
> 
> > Uh... that perl is way over my head.  I admit though, that perl's
> > powerful substitute command is also clumsy.  The best I can do
> > right now is:
> >
> > $v =  "I have a dream\n";
> > $v =~ s/have/had/;
> > print $v
> 
> I was going to suggest just using a function. But never having coded in 
> Perl before, I wasn't expecting something this ugly:
> 
> sub replacewith{
>     $s = $_[0];
>     $t = $_[1];
>     $u = $_[2];

I think [untested] you can shorten those 3 lines to:
($s, $t, $u) = @_ ;

>     $s =~ s/$t/$u/;
>     return $s;
> }



More information about the Python-list mailing list