Basically, what is happening is that when greedy constructs fight, the left one wins. In your pattern  m/(.*)fish(.*)/) There are two (.*) constructs that try to get as many characters as they can without failing the match. The first one gobbles up until the last fish (because if it ate the fish, your "fish" couldn't match), then the "fish" matches, and only then is the last (.*) let to grab as much as it can.

As stated above, you either want the non-greedy version in front (if you want to split the string only on the first "fish") or the split function (which is appropriate if you don't mind splitting on each fish in the line).

Also as stated above, read perlre early and often. I've read it in the last week and I was still thrown for a loop by Corion's post above, my interpretation of the pattern was very wrong.


In reply to Re: match last element instead of first?? by amarquis
in thread match last element instead of first?? by Anonymous Monk

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.