[Memphis.pm] RE: [GOLUM] How do I...?

J-L Boers jl at boers.cc
Mon Jun 18 20:42:19 CDT 2001


I'll try that again when I get the chance.

What's a good "perl for dummies" type book to get? I may want to play around
a little more in depth with it. I think a step-by-step approach teaching the
command structure (or whatever you call it) would be a good way to start.
Any suggestions?

-----Original Message-----
From: golum-admin at golum.org [mailto:golum-admin at golum.org]On Behalf Of
Brock Sides
Sent: Monday, June 18, 2001 8:31 PM
To: golum at golum.org; memphis-pm-list at pm.org
Subject: Re: [GOLUM] How do I...?


* J-L Boers <jl at boers.cc> [010618 06:49]:

> Thanks Brock,
>
> I made use of the perl script as you wrote it. It worked beautifully. I
> did a mkisofs of the directory afterwards and burned a cd. the cd works
> great under Linux and Windows. the navigation part is a little awkward
> as the html file did not come out in alphabetical order. (i really don't
> know how it sorted itself, creation date maybe???)
> but that's a minor detail. Great script!

Oh, you wanted the output sorted? :)

Try this (in bash):

#!/bin/bash

cat >index.html <<FIN
<html>
<body bgcolor=white>
<h1>My Pictures</h1>
FIN

for i in `ls *.jpg *.png *.gif | sort` ; do
   echo "<a href=$i>$i</a><br>" >>index.html
done

cat >>index.html <<FIN
</body>
</html>
FIN

Or in Perl:

#!/usr/bin/perl -w

open(FH, ">index.html") or die $!;

print FH <<FIN;
<html>
<body bgcolor=white>
<h1>My Pictures</h1>
FIN

opendir(DH, '.') or die $!;

print FH map "<a href=$_>$_</a>\n", sort grep /\.(jpg|gif|png)/, readdir DH;

print FH <<FIN;
</body>
</html>
FIN

__END__

This will sort asciibetically, which may not be what you're looking for.
To sort alphabetically, use this magic incantation instead:

print FH map "<a href=$_>$_</a>\n",
   sort {uc $a cmp uc $b}
   grep /\.(jpg|gif|png)/, readdir DH;

(To sort alphabetically in the bash script, use "sort -f" instead of
just "sort".)

This might be a good time to introduce the lispiest of perl functions,
map and grep. grep, like the unix command, takes a list and
creates another list, by selecting elements that fit a certain
condition. However, in perl this condition need not be a regex to match,
but can be any condition. E.g., I can select the even numbers from 1 to
100 like this:

@evens = grep {$_ % 2 = 0} 1..100;

map is similar, but rather than selecting elements from the list that
fit a condition, it creates new elements. E.g. here's a way to generate
the first 100 squares:

@squares = map {$_ ** 2} 1..100;

> BTW.... What does "TMTOWTDI, of course" stand for.

That's the Perl motto: There's more than one way to do it.

--
Brock Sides
philarete at mindspring.com

One OS to rule them all, one OS to find them,
One OS to bring them all and in the darkness bind them,
In the land of Redmond, where the shadows lie.

_______________________________________________
Golum mailing list
Golum at golum.org
http://lists.golum.org/mailman/listinfo/golum

----------------------------------------------------------------------------
To unsubscribe, please send email to majordomo at pm.org
with 'unsubscribe memphis-pm-list' in the body of the message.
----------------------------------------------------------------------------




More information about the Memphis-pm mailing list