[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Drunk Driving redux (what was he thinking?)



At 20:50 -0400 06/28/1999, Steve Willer wrote:
>
>Two regular expressions and an index, when he only needs one regexp? Ick.
>Here's my try:
>
>  opendir(phddir, $dirpath) || die "couldn't open directory";
>  while (my $fname = readdir(phddir)) {
>    $fname =~ s|^.*/([^/])\.phd\..*$|$1|g or next;
>    $phdfiles{$fname} ||= '';
>  }
>  closedir(phddir);
>
>Is that better? I write a lot of Embperl-based web pages with forms, so I
>absolutely love the ||= operator.

There we go :-)

I am somewhat chagrined to admit that in my distaste for the use of
index() I had completely missed the opportunity to fold the matches
into one expression on my first go-round.  Upon awakening this morning,
I had that realization (hmmm... before I even got out of bed I was thinking
Perl... consider the implications... :-)

My thoughts ran to:

   while(defined($file = readdir(DIR))) {
      if ($file =~ m#^(.*)\.phd\.# ) {
         $Root = $1;
         $phdFiles{$file} = 1 unless (exists($phdFiles{$file});
     }
   }

noting that the following substitution from the original code
         $szRoot =~ s/^.*\///;
is unnecessary because the data comes from readdir (not ls) and thus the
resulting filenames will be simple filenames, not full pathnames (hence, no
stuff "up to the last slash in the name" to remove).

As for Steve's suggestion about setting the hash elements:

     $phdfiles{$fname} ||= '';

Given that this is the place in the script where we're initializing the
hash (that isn't obvious from the snippet, but I'll tell you it's the
case), if we use

     $phdfiles{$fname} ||= 1;

would that not address both Randal's valid concerns RE: truth and data as
well as Steve's appreciation of ||= ?

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org