double substitution
am 20.09.2011 02:56:54 von Rajeev Prasad
$string="alpha number='42'"
$string=~s/.*\=// ;
$string=~s/\'//g;
to get 42 and not '42'
can these two substitutions be combined?
thank you.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: double substitution
am 20.09.2011 03:10:34 von Rob Dixon
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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: double substitution
am 20.09.2011 03:17:49 von Jim Gibson
On 9/19/11 Mon Sep 19, 2011 5:56 PM, "Rajeev Prasad"
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;
}
or shorter (but not better):
($number) = $string =~ /'(\d+)'/;
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: double substitution
am 20.09.2011 03:43:52 von Rob Dixon
On 20/09/2011 02:17, Jim Gibson wrote:
> On 9/19/11 Mon Sep 19, 2011 5:56 PM, "Rajeev Prasad"
> 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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: double substitution
am 20.09.2011 15:07:47 von Shawn H Corey
On 11-09-19 08:56 PM, Rajeev Prasad wrote:
> $string="alpha number='42'"
> $string=~s/.*\=// ;
> $string=~s/\'//g;
>
> to get 42 and not '42'
>
>
> can these two substitutions be combined?
>
> thank you.
>
>
It depends on what you want to extract. To extract a string inside
single quotes after an equal sign:
$string =~ s/.*\=\s*\'([^']+)\'/$1/;
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
"Make something worthwhile." -- Dear Hunter
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/