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

love smartmatch! but is there way to get the index value of what your just matched? as best as i've been able to tell smartmatch only does a boolean return.

Replies are listed 'Best First'.
Re: smartmatch and index
by kcott (Archbishop) on Oct 13, 2010 at 01:01 UTC

    In perlsyn (under Switch statements) there's a sub-section called Smart matching in detail. Here you'll find a table of all the smart match combinations. They're all boolean.

    -- Ken

Re: smartmatch and index
by ikegami (Patriarch) on Oct 13, 2010 at 02:08 UTC

    Index doesn't even make sense for most smart-match inputs, so it doesn't make sense for smart-matching in general.

    This is probably what you want:

    my @indexes = map { EXPR ~~ $a[$_] } 0..$#a;
      hopefully we're not arguing semantics...but i think being able to get the index (array 'position') based on the result of a positive smartmatch is very useful. knowing that ( "foo" ~~ @big_array ) is true is helpful (and we rejoice in how fast it is). in some cases knowing that "foo" is at index 346456 may be really really helpful. i realize that "foo" could appear repeatedly and in that case the map example is a good solution. it would be nice if the smartmatch function could be a bit more "telling".

        i think being able to get the index (array 'position') based on the result of a positive smartmatch is very useful.

        Indeed, and I showed you how to do it.

        it would be nice if the smartmatch function could be a bit more "telling".

        Smart match has nothing to do with arrays. It can't return indexes when most match rules don't even involve indexes.

Re: smartmatch and index
by LanX (Saint) on Oct 13, 2010 at 00:29 UTC
    code example?