On 20/09/2011 02:17, Jim Gibson wrote:
On 9/19/11 Mon  Sep 19, 2011  5:56 PM, "Rajeev Prasad"<rp.ne...@yahoo.com>
scribbled:

$string="alpha number='42'"
$string=~s/.*\=// ;
$string=~s/\'//g;

to get 42 and not '42'


can these two substitutions be combined?

If you know what you want to extract, then use capturing:

if( $string =~ /'(\d+)'/ ) {
     $number = $1;
}

I prefer to always overemphasize the environment that 'use strict'
applies, under which your code will not compile. In fact you would need
to declare $number before the if block for it to be useful:

  use strict;

  my $number;
  if ( $string =~ /'(\d+)'/ ) {
    $number = $1;
  }

or shorter (but not better):

($number) = $string =~ /'(\d+)'/;

Neither solution is wrong as a cameo, but both expect a string of digits
surrounded by single quotes. Given Rajeev's coding I think it is safer
to assume that the equals sign is significant.

I know these comments is excessively picky, but they are the difference
between code that works and code that doesn't fail.

Rob


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to