If you really need a function which gives you the truth value of m//, but returns the index, you can always write something like
sub match_index { my $pos; return (($pos =index($_[0],$_[1]))<0) ? 0 : ($pos == 0) ? '0 but true' : $pos; }
Note that "0 but true" is special syntax that does not emit a warning when you add 1 to it. "0 but True" or "0 but TRUE" don't have this property.

On the machine I'm on right now, the sub takes less than a microsecond, so you have to be doing a lot of matches for it to matter. (m// takes a quarter of that time, and index() takes an eighth.)

If the problem is just that index() didn't work like you expected it to, then, as the others suggest, you need to change what you expect. For what it's worth, both index() and m// work as I expect them to work, and my sense of the other posters so far is that that's mostly true for them. As they say in the U.S. auto business, "Your milage may vary."


In reply to Re: Perl index() Function Returns String, Not Numeric by rodion
in thread Perl index() Function Returns String, Not Numeric by ironmo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.