in reply to Re: My regex works, but I want to make sure it's not blind luck
in thread My regex works, but I want to make sure it's not blind luck
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.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?
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.
|
|---|