in reply to Alternative matches

Isn't index() often faster for this sort of fixed-string search? If so, would it be better to chain a series of calls to index() with the || operator? (Except that since you want case-insensitivity, you would want to lc($string) first.) Just pondering . . . .

Replies are listed 'Best First'.
Re^2: Alternative matches
by PodMaster (Abbot) on Oct 06, 2004 at 11:18 UTC
    No.
    1) You wouldn't want to chain so many calls to index (its like writing $array[0]=3; $array[1]=4; $array[1]=5;)

    2) Notice the use of capturing parenthises? It means that kiat wants to know what word he matched. To do that with index gets ugly real fast, for example

    my @words = qw[ new old number start simple cross heavy die exit ]; my $string = q~whatever the NEw old you're trying to match~; my $lcstring = lc $string; my $ret = 0; if( do { for my $val( @words ){ my $r = index $lcstring, $val; if( $r > -1 ){ $ret = substr $string, $r, length $val; last; } } $ret; } ){ warn "the \$1 (aka ret) is => $ret\n\n"; } __END__ the $1 (aka ret) is => NEw
    Granted you could turn that do{} into a function, but I don't think its worth the effort (even if there may be a slight speed boost).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Oops. i missed the capturing parentheses . . . . *blushes* However, outside of that, i'm not clear on why one wouldn't want to do something like:
      if ( index($string, 'new') || index($string, 'old') || index($string, 'number') || # etc. ) { # etc. }
      except that it does seem ugly to me, now i look at it. :-) Could you elaborate, please?
        *boggle* Take a look at Re^2: Alternative matches again. Then try perldoc -f index.
        What you'd need to actually write is
        index($string,'new') > -1 ||
        for however many words there are. You'd have to modify that if for every new word you added, which would soon turn into
        index($string,'new') > -1 || index($string,'old') > -1 || index($string,'number') > -1 || index($string,'start') > -1 || index($string,'simple') > -1 || index($string,'cross') > -1 || index($string,'heavy') > -1 || index($string,'die') > -1 || index($string,'exit') > -1 || ...
        That is a lot of cut'n'pasting and this is not what programmers do. What you'd do is write a function to loop over an array like I did.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

      Err...

      $r = index $lcstring, $val;; $ret = substr $string, $r, length $val;
      Seems like a very obfuscated way of saying $ret = $val if -1 < index $lcstring, $val;
      No?
        No. Pay attention:
        my $string = q~whatever the NEw old you're trying to match~;

        my $lcstring = lc $string;

        the $1 (aka ret) is => NEw

        $string ne lc $string

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.