In Perl, you can use the index of
[-1] to indicate the last array element. A REGEX would work fine here also. I don't know without benchmarking, but I suspect that the regex version is actually faster.
#!/usr/bin/perl -w
use strict;
my $line ='last -- From --00--SPLIT ?--11452';
my ($last) = (split(/--/, $line))[-1];
print "using split: $last\n";
($last) = $line =~ /(\d+)\s*$/;
print "using regex: $last\n";
__END__
Prints:
using split: 11452
using regex: 11452
Update: I guess I should say that the parens around ($last) are necessary to put $last into a list context, otherwise you just get 1 or 0 value. Here we want the actual value that was captured rather than whether it the expression "worked" or "not.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.