ironmo has asked for the wisdom of the Perl Monks concerning the following question:
Believe it or not, you actually have to force a numeric context to get it to work:my $string = 'string here'; my $index = index $string, 'bah'; print $index if $index;
print $index if $index >= 0;Beware of this one when using index as a faster alternative to regular expressions when simply checking for keywords in a string. The following is particularly subtle and annoying:
That block of 'if' code will always be executed. And here I thought I'd run into my last internal perl bug, reminds me of the 90s again.if (my $index = index $string, $key) { # yadda }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl index() Function Returns String, Not Numeric
by ikegami (Patriarch) on Aug 19, 2006 at 07:26 UTC | |
by tilly (Archbishop) on Aug 19, 2006 at 07:49 UTC | |
|
Re: Perl index() Function Returns String, Not Numeric
by tilly (Archbishop) on Aug 19, 2006 at 07:16 UTC | |
by GrandFather (Saint) on Aug 19, 2006 at 07:38 UTC | |
|
Re: Perl index() Function Returns String, Not Numeric
by rodion (Chaplain) on Aug 19, 2006 at 10:44 UTC | |
by tilly (Archbishop) on Aug 19, 2006 at 20:34 UTC | |
by ikegami (Patriarch) on Aug 19, 2006 at 20:41 UTC | |
|
Re: Perl index() Function Returns String, Not Numeric
by BrowserUk (Patriarch) on Aug 19, 2006 at 11:22 UTC | |
|
Re: Perl index() Function Returns String, Not Numeric
by chromatic (Archbishop) on Aug 19, 2006 at 19:13 UTC |