Well, it's a way, but I would write the block as:
for (@files) { my @matches = /(.{8}-.{3}-i32)/g; my $last_match = $matches [-1]; print $last_match, "\n"; }

You're not doing anything with $match, and I wouldn't assing the @_. Furthermore, why use @stats if all you care about is a single scalar? It's confusing and is less efficient. Also, if you are going to match against $_, no need to bind it. Note also that there is no need to backwhack a dash in a regular expression outside of character classes.

But you can eliminate the intermediate array:

my $last_match = (/(.{8}-.{3}-i32)/g) [-1];

Or, if you are certain there is a match:

1 while /(.{8}-.{3}-i32)/g; my $last_match = $1;
You might even be able to use:
my ($last_match) = /.*(.{8}-.{3}-i32)/;
although in some cases that might be slower, or (in case of possible overlapping matches) give you a different answer.

The latter two have the advantage that they don't create an intermediate array or list, which, if there are lots and lots of matches, could be a win.

Abigail


In reply to Re: Matching last value in reg expression by Abigail-II
in thread Matching last value in reg expression 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.