On 20/09/2011 01:56, Rajeev Prasad wrote:

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

to get 42 and not '42'


can these two substitutions be combined?

Hi Rajeev

Well they can be combined, and because it is far from obvious what your
code is doing I think it should be rewritten.

Using s/// to modify your original string in place is probably a bad
idea. I think you should capture the data you need into a separate
variable.

I suggest something like the code below, but its validity depends
completely on what variations you can expect from you data source. It
looks for an equals sign, possibly followed by non-digits, and then a
sequence of at least one digit which it captures.

HTH,

Rob


use strict;
use warnings;

my $string = "alpha number = '42'";

my ($n) = $string =~ /=\D*(\d+)/ or die "No alpha number";

print $n;

__END__

**OUTPUT**

42

--
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