Thursday, August 18, 2005

perl lesson

How to turn this list:

Zwan:Mary Star Of The Sea
k.d. lang:Absolute Torch and Twang
k.d. lang:Hymns Of The 49th Parallel
k.d. lang:Invincible Summer
k.d. lang:Shadowland

into this list:

ZWAN
Mary Star Of The Sea
K.D. LANG
Absolute Torch and Twang
Hymns Of The 49th Parallel
Inncible Summer
Shadowland

---------------------------------

#!/usr/bin/perl -w

$header ="random text";

foreach $line (<>)
{
if ($line =~ /$header/)
{
$line =~ s/$header/ /;
}else{
$line =~ s/(.*\:)/\U$1/;
$header = quotemeta $1;
$line =~ s/\:/\n\t/; #indent album
}
print $line;
}


It took me a couple of days to figure out i had to use "quotemeta" to
escape all those random, special characters that might appear in any
list.

3 comments:

gl. said...

that's really pretty. i wish i could perl. *sighs dreamily*

xire said...

do you know how to use vi and regular expresions?

gl. said...

definitely not vi, and regular expressiosn only minorly.