Hacker News new | ask | show | jobs
by petre 1709 days ago
Well, that was inspired by sed. I have to do a test in Ruby too see how fast regexos are, being spoiled by Perl usage and all. What I don't like is this:

  my $a = 'Perl';
  my $b = $a;
  $b =~ s/pe/ea/i;
Why not do something to $a and obtain $b, just like js does with Array.map. Why the need to copy $a into to $b and then replace? Much more ellegant in Ruby:

  a = 'Ruby'
  b = a.gsub(/ru/i, 'mo')
2 comments

oh— for regex performance Perl blows Swift, Java, Python, Ruby, Go, and C++ out of the water. For simple string manipulation— substring replacement, string reversal, etc. it's never the worst but never the best. Scroll down to the bottom of this paper where they test actual regexes though and the difference is pretty stunning.

http://jultika.oulu.fi/files/nbnfioulu-202001201035.pdf

No need to copy and then replace:

  my $a =  'Perl';
  my $b =~ s/pe/ea/ir;
Doesn't work. You don't assign anything to $b, so it's undef:

  % perl -MData::Dumper=Dumper -e 'my $a = 'Perl'; my $b =~ s/pe/ea/ir; print Dumper $b;'
  $VAR1 = undef;
You probably mean:

  my $a = 'Perl';
  my $b = $a =~ s/pe/ea/ir;
Anyway, thank you for the /r modifier, it didn't know what it did, since there's no example in perlre(1).
You're right, that's exactly what I meant!

I'm surprised there's no example in perlre(1); perhaps I can get that corrected.