in reply to Looking for N/A twice

Off-topic, but I realised that you could search for two or more occurrences of a substring in a string with nested index() calls like this:
$str = 'N/A N/A blah blah blah SATA N/A blah blah N/A'; $substr = 'N/A'; if (index($str,$substr, index($str,$substr)+1 ) ) { print "two or more occurrences of '$substr' in '$str'" }
which is probably way faster than any RegEx method.


Nobody says perl looks like line-noise any more
kids today don't know what line-noise IS ...

Replies are listed 'Best First'.
Re^2: Looking for N/A twice
by ambrus (Abbot) on Oct 22, 2007 at 09:07 UTC

    No way. index($str,$substr, index($str,$substr)+1 practically always returns true, even if $str has $substr only once or not at all. I don't think it's way faster than regexen either.