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

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



At 14:50 -0400 6/28/99, Ronald J Kimball wrote:
> On Mon, Jun 28, 1999 at 11:20:20AM -0700, Vicki Brown wrote:
>> Specifically, the line which illustrates (to me) TMTOWTDI (but some are
>> wrong) is:
>>     if ( index( $szPhdFile, ".phd." ) >= 0 ) {
>
> How does this line illustrate that some ways are wrong?  This line of code
> works just fine.

I never said it didn't work. I said that just because TIMTOWTDI some of
those ways are wrong.  Wrong as in "inappropriate" not wrong as in "that
would fail".

>> I would have used
>>
>>     if ($szPhdFile =~ /\.phd\./) {
>>

> Why would someone who understands index use pattern matching to find a
> constant substring?

Good question -- Because he doesn't understand =~ m/pattern/?  :-)

I don't think that's the operative question. I think the question is,
rather, why would someone who wants to match a pattern do so using index()?

The code is testing only that the substring exists in the string. The index
value (position) never gets used except to check that it is >= 0.

> What were you thinking?  *grin*

Clarity of expression :-)

When I use index it's because I want to know the position of one string
inside another.  When I see index() I conclude that the _value_ of the
index, i.e. the position of the substring, is important. I use index if I
want to determine _Where_.

I don't use index() when I simply want to know the truth value (is string a
contained in string b). When I want to know the latter (and I don't care
where, only if), I use a pattern match. I use pattern matching if I want to
determine _If_.

Maybe I'm just odd (OK. perhaps I should rephrase that... :) Using index()
in this instance doesn't seem very... Perlish... somehow. It's also (imo)
misleading.

> It appears that index is faster for short target strings, but regex is
> faster for long target strings.  (With perl5.004, anyway.)

As should be obvious from some of my other posts, I tend to value Making
the Goal Clearer to the Reader over speed (at least when the speed issue is
nearly moot).

For those who can't recall the original code:

  opendir( dirPhdDir, $phdDirPath ) || die "couldn't open directory";
  while( defined( $szPhdFile = readdir( dirPhdDir ) ) ) {
    if ( index( $szPhdFile, ".phd." ) >= 0 ) {
      ( $szRoot = $szPhdFile ) =~ s/\.phd\..*$//;
      $szRoot =~ s/^.*\///;
      if ( ! exists $aPhdFiles{ $szRoot } ) {
        $aPhdFiles{ $szRoot } = "";
      }
    }
  }
-- --
       |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
 ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
      |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
     '---''(_/--'  `-'\_) http://www.cfcl.com/~vlb  http://www.macperl.com

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