I guess if we are going to beat this thing to death, split() could also be used:
use strict; use warnings; my @images = qw{a.png b.gif c.svg d.blah.jpg ..}; foreach (@images) { my $after_last_dot = (split (/\./,$_))[-1]; $after_last_dot //= ''; print ".",$after_last_dot,"\n"; } __END__ .png .gif .svg .jpg . <=might want something else here?
I believe that the substr, rindex approach will be by far the fastest - these are very simple functions. The regex will be slower, but in my opinion, it is much easier to understand and I would prefer it for that reason. For most of my work, the speed difference would not be of any significance what-so-ever. There are of course always exceptions if you do something enough times! I suppose that split() performance would possibly wind up performance wise somewhere in-between? Although without benchmarking, I can't be sure. It could actually be slower than the first regex method because of more things pushed onto the output array.

Anyway in the spirit of "more than one way to do it", see split() solution. I did add code to handle the "undefined" case. The //= operator is a cool thing.


In reply to Re^2: My regex works, but I want to make sure it's not blind luck by Marshall
in thread My regex works, but I want to make sure it's not blind luck by SergioQ

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.