On 22/07/2011 16:05, Mike McClain wrote:
On Thu, Jul 21, 2011 at 02:11:33PM -0700, Mike McClain wrote:
Given the following snippet of code could someone explain to me
why the linefeed gets included in $num?
mike@/deb40a:~/perl> perl -e'
$str = "asdfggfh 987321qwertyyy\n";
($num = $str) =~ s/^.*?(\d+).*$/$1/;
print $num;
' | hd
My thanks to Shawn, Jim, Rob& Dr. Rudd for their answers.
Now I not only know why I was getting the '\n' but 2 ways to avoid it.
($num = $str) =~ s/^.*?(\d+).*$/$1/s;
or
($num) = $str =~ /^.*?(\d+).*$/;
Again, there is no need for all that noise in the regex.
my ($num) = $str =~ /(\d+)/;
is fine.
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/