in reply to Re^2: Alternative matches
in thread Alternative matches

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?

Replies are listed 'Best First'.
Re^4: Alternative matches
by PodMaster (Abbot) on Oct 06, 2004 at 11:57 UTC
    *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.

      I wrote this code assuming the regexp would still be faster, and I wanted to prove it. I was shocked by what I found. I'm borrowing some of PodMaster's benchmark code. If there's something wrong with my testing let me know, but it sure looks like the index solution blows the regexp out of the water.
      #!/usr/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; use List::Util 'first'; my @words = qw[ new old number start simple cross heavy die exit ]; my $string = join ' 0\4/f ', map( { rand $_ } 1 .. 60), map { $words[ +rand @words ] } 1 .. 20; cmpthese (-3, { regexp => sub { return $string =~ /(new|old|number|start|simple|cross|heav +y|die|exit)/i; }, use_index => sub { my $lcstring = lc $string; return first {index($lcstring, $_) > -1} @words; } }); __END__ Rate regexp use_index regexp 1084/s -- -96% use_index 29469/s 2619% --
        The benchmark looks fine, but if $string is 'nEw', regexp will return 'nEw', while use_index will return 'new'.

        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.

      Thanks for clarifying that. But i wonder, do Real Programmers also try to avoid making assumptions about what they're working with? Because i think you meant to tell me to try man perlfunc . . . . or indeed, to read the Camel more carefully. i'm tired and lacking in concentration tonight; i think i'll take my pseudo-programmer self away and just spectate from now on.
        perldoc is better (even this tutorial reccomends it).

        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.