how do I get the replacement expression(s) to be evaluated

I

inderpaul_s

Here is my code and what I want to do is replace both numbers with
their increments by 1...so I've got the following expression but I can
only get the first number to increment by one: I've written the code to
change the next number further below:

========== this works to increment 32000000 to 32000001============

my $string28 = "There are 32000000 people in Canada, an average of
2600000 per province";
my $string29 = "There are 32000000 people in Canada, an average of
2600000 per province";

if($string29 =~ s/(\d+)/$1 + 1/e)
{
print "\$string28 before is ==> $string28\n";
print "\$string29 after is ==> $string29\n";
}

====== this does NOT work to increment both 3200000 and 2600000 by
1=======

if($string31 =~ s/(\d+)[^\d]+(\d+)/$1+1,$2+1/e)
{
print "\$string30 before is ==> $string30\n";
print "\$string31 after is ==> $string31\n";
}
 
I

inderpaul_s

Well what do you know...I got the second part to work...with the
following solution:

if($string31 =~ s/(\d+)/$1+1/ge)
{
print "\$string30 before is ==> $string30\n";
print "\$string31 after is ==> $string31\n";
}
 
M

Mintcake

Well what do you know...I got the second part to work...with the
following solution:

if($string31 =~ s/(\d+)/$1+1/ge)
{
print "\$string30 before is ==> $string30\n";
print "\$string31 after is ==> $string31\n";
}
Clearly, the /g option is best in this case. But why did the original
script not work?
if($string31 =~ s/(\d+)[^\d]+(\d+)/$1+1,$2+1/e)

Without the /g this will perform a single sibstitution. The part of
the original string which matches the regex will be

32000000 people in Canada, an average of 2600000

$1 and $2 will be 32000000 and 2600000 as expected

The value that will replace the matched string is the expression
$1+1,$2+1. Because of the comma operator this evalutes to $2+1

So you get:

There are 2600001 per province

For the general case (i.e. if /g would not work say because the two
bits of regex that matches the numbers were different) you could use...

s/(\d+)([^\d]+)(\d+)/($1+1) . $2 . ($3+1)/e

Here we also capture the middle bit of the string

" people in Canada, an average of "

and the substitute expression uses concatenation. Note the parentheses
around the additions is necessary because of operator precedence.
 
M

Mintcake

Well what do you know...I got the second part to work...with the
following solution:

if($string31 =~ s/(\d+)/$1+1/ge)
{
print "\$string30 before is ==> $string30\n";
print "\$string31 after is ==> $string31\n";
}
Clearly, the /g option is best in this case. But why did the original
script not work?
if($string31 =~ s/(\d+)[^\d]+(\d+)/$1+1,$2+1/e)

Without the /g this will perform a single sibstitution. The part of
the original string which matches the regex will be

32000000 people in Canada, an average of 2600000

$1 and $2 will be 32000000 and 2600000 as expected

The value that will replace the matched string is the expression
$1+1,$2+1. Because of the comma operator this evalutes to $2+1. So
you get:

There are 2600001 per province

For the general case (i.e. if /g would not work say because the two
bits of regex that matches the numbers were different) you could use...


s/(\d+)([^\d]+)(\d+)/($1+1) . $2 . ($3+1)/e

Here we also capture the middle bit of the string

" people in Canada, an average of "

and the substitute expression uses concatenation. Note the parentheses
around the additions is necessary because of operator precedence.

This is ugly though. Unless there's a good reason for doing it this
way, two separate s/.../.../ statements are clearer.
 
T

Tad McClellan

replace both numbers with
their increments by 1

my $string28 = "There are 32000000 people in Canada, an average of
2600000 per province";


$string28 =~ s/(\d+)/$1 + 1/ge;
 
T

Tad McClellan

Here is my code
my $string28
my $string29
[snip]

print "\$string30 before is ==> $string30\n";
print "\$string31 after is ==> $string31\n";


Having a bazillion individual scalars with incrementing numbers
in there names is a red flag that indicates a poor design.

You should be using one array with 31 elements rather than 31
individually named scalar variables.
 
G

Guest

: > Here is my code

: > my $string28
: > my $string29

: [snip]

: Having a bazillion individual scalars with incrementing numbers
: in there names is a red flag that indicates a poor design.

: You should be using one array with 31 elements rather than 31
: individually named scalar variables.

Or use a hash with 31 different keys; perhaps his data is by province,
so $string{'Ottawa'} could make sense, and it would be an even better
mnemonic. Just my 2 cents.

Oliver.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top