[Perl] Word/Text Manipulation

Reuven M. Lerner reuven at lerner.co.il
Sun Jun 23 23:42:13 PDT 2002

>>>>> "Georges" == Georges EL OJAIMI <G-OJAIMI at cyberia.net.lb> writes:

    Georges> 1) I want to convert all occurrences of the letters:
    Georges>    o,j,a,i,m,i to
    Georges> uppercase!

How about:

    $text =~ tr/ojami/OJAMI/;

    Georges> 2) I want to reverse the order of the letters of a word!

You want to reverse every word in a file?  Well, how about:

    perl -p -e 's|\b(\w+)\b|reverse $1|eg;' FILENAME

This looks for one or more word element characters (\w), capturing
them into the variable $1 (because it's the first set of parentheses).
We then replace that word with the result of invoking "reverse $1".


More information about the Perl mailing list