lufthansa has asked for the wisdom of the Perl Monks concerning the following question:

The statement : if (not (index STRING,SUBSTRING)) return true if the substring IS included in string
  • Comment on How could I check if substring is not included in string ?

Replies are listed 'Best First'.
Re: How could I check if substring is not included in string ?
by Corion (Patriarch) on Jan 26, 2009 at 10:39 UTC

    The documentation says:

    If the substring is not found, index returns one less than the base, ordinarily -1 .

    So you will need to modify your test accordingly.

Re: How could I check if substring is not included in string ?
by moritz (Cardinal) on Jan 26, 2009 at 10:39 UTC
    Read the documentation for index, it returns -1 on failure to find the substring.

    So the comparison should be index(...) == -1

    Btw Perl 6 makes this more intuitive by introducing a different boolean context, so that index can return a StrPos object that's true even if the substring was found at position 0. (Yes, Perl 5 could also make it return "0 but True", but "-1 but False" doesn't work).