Even if you were more comfortable with regular expressions, split would have been a good choice for this problem.
However, getting comfortable with regular expressions will open up a world of programming possibilities to you. It is worth investing the effort, even if it feels intimidating at first. One way to build confidence is to see problems you already understand well reworked into regular expressions, so here it is:
my ($lastWord) = $rec =~ /(\S+)$/;
print "$lastWord\n"; #prints 96
#or
$rec =~ /(\S+)$/;
my $lastWord = $1;
print "$lastWord\n"; #also prints 96
- The final '$' means whatever we match has to end with the end of the string.
- \S stands for not a space, i.e. anything except characters that would match \s.
- \S+ means match as many as non-spaces as you can, that is, all the way back to (but not including) the first space.
- (\S+) means capture this portion of the match and put it in a variable. Perl automatically puts each captured match portion in numbered variables: the first captured match in $1, the second in $2. In this case we had only one captured match, so $1 contains the match. Had you had two matches, like this $rec =~ (\S+)\s+(\S+)$ then the regex would have set $1 to "-!" and $2 to "96".
- $1 and $2 are very transient variables. They will change the next time you do a regular expression match or substitution that has a captured portion, so it is usually a good idea to copy them into one of your own declared variables as soon as possible. One easy way to do this is to assign the regex matching expression ($rec =~ /(\S+)$/) to an array or list, e.g. my ($lastWord) = $rec =~ /(\S+)$/.
And we even could have combined your trailing space tripping code with the regular expression as well. This also would have worked: my ($lastWord) = $rec =~ /(\S+)\s*$/. \s* is like \s+ except that it matches zero or more spaces, rather than one or more. That is \s+ is equivalent to \s\s*.
For more information and some further examples, see perlretut.
Best, beth
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.