in reply to Non-regex Methods of Extracting the Last Field from a Record

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

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