You can also use a regular expression. You were almost
there with the code you had. Here's a slight rewrite:
my $mydir = 'Where/the/directory/is';
opendir (MYDIR, $mydir) or die "Could not opendir $mydir: $!\n";
my @numfiles = grep { /^\d{4}.*\.html$/} readdir MYDIR;
closedir(MYDIR);
print "<UL>\n";
for (@numfiles) {
print qq! <LI><A HREF="$_">$_</A></LI>\n!;
}
print "</UL>\n";
Quick notes:
- Always check the return of your opens and opendirs.
There is no point in the script continuing if they fail.
- The regular expression in this case matches exactly 4 digits "\d{4}"
at the very start of the string "^" followed by anything ".*" and
ending in 'dot-html' at the very end of the string "\.html$"
- If your HTML has double quotes, you can make them without any problems
by using an alternative quoting character ("!" in this case) with the
qq operator. See the section entitled "Quote and Quote-like Operators"
in perldoc perlop.
- Using <LI> is a quick and easy way to create a nice
formatted list - like this one! :)