[LA.pm] String handling like C array
Peter Benjamin
pete at peterbenjamin.com
Fri Feb 3 18:08:04 PST 2006
You're looking to learn regular expressions.
Typically, 10 lines of C string code is just 1 line of perl.
So, the functionality your post showed would be:
$str =~ s/x/y/g;
$str =~ s/y/x/g;
s is a perl command for "stream editing". It automatically
loops through each character of string. Perl has several
commands for this.
The tr command combines these two lines into one line.
See a previous post for that one. tr stands for translate.
The first // pair delimits the "match pattern,"
and the second // pair (re-using the last /
of the first pair) delimits the "substitution."
You can learn more about REs (for the novice or
if you are having troubles reading or writing them) at:
http://peterbenjamin.com/seminars/regexp/regular.expressions.lesson2.1.html
Once you can read a RE, writing simple ones is easy and fast.
Hmm, that is 2.1, and I know I did 2.2 last year.
I'll see about getting online after tomorrow, as
I speak at www.LAWebSpinners.org on Saturday.
More information about the Losangeles-pm
mailing list