Is Perl *that* good? (was: How's ruby compare to it older brother python)

Ville Vainio ville at spammers.com
Tue Apr 27 02:46:45 EDT 2004


>>>>> "Leif" == Leif B Kristensen <junkmail at solumslekt.org> writes:

    Leif> Here's a small sample:

    Leif> $str =~ s/HN/N/g;        # John --> JON
    Leif> $str =~ s/TH/T/g;        # Thor --> TOR
    Leif> $str =~ s/CHI/KJ/g;      # Torchild --> TORKJL

...

Isn't that trivial in all languages that support regexps?

All too often Perl gets too much credit for doing things that are
every bit as easy/easier in other languages.

As a bonus, this python snippet returns the changes done for every
individual regex pair:


-------------
import re

repls = [
 ('NH','N'),
 ('TH','T'),
 ('CHI','KJ'),
 ('hel.*o','ll')  # one regex just for show

]

def subst_many(pairs, s):
    res = []
    for pat,repl in pairs:
        s, n = re.subn(pat, repl, s)
        res.append(n)
    return s,res

result, changes_done = subst_many(repls, my_text_file)








-- 
Ville Vainio   http://tinyurl.com/2prnb



More information about the Python-list mailing list