multiplication within RegExp.

A

Andreas

Hi!

I have a Perl-problem, and I'm really not a Perl-person... Can you help me?

I want to translate a part of a line that looks like:
"...3*DATAWIDTH*4..." to "...24...".

I have the variables
$define_word = "DATAWIDTH".
$define_value = 2

The expression (without the new line):
$row =~ s/([0-9]+)\s*\*\s*$define_word\s*\*\s*([0-9]+)
/$define_value*$1*$2/gi;

just give me the string "...2*3*4...". How can I make this "...24..."

best regards,
Andreas Lundgren
 
G

gnari

Andreas said:
Hi!

I have a Perl-problem, and I'm really not a Perl-person... Can you help me?

I want to translate a part of a line that looks like:
"...3*DATAWIDTH*4..." to "...24...".

I have the variables
$define_word = "DATAWIDTH".
$define_value = 2

The expression (without the new line):
$row =~ s/([0-9]+)\s*\*\s*$define_word\s*\*\s*([0-9]+)
/$define_value*$1*$2/gi;

just give me the string "...2*3*4...". How can I make this "...24..."

have a look at the /e modifier as in s///e;

perldoc perlop
(find s/// in the chapter "Regexp Quote-Like Operators")
 
C

Chris

Andreas said:
Hi!

I have a Perl-problem, and I'm really not a Perl-person... Can you help me?

I want to translate a part of a line that looks like:
"...3*DATAWIDTH*4..." to "...24...".

I have the variables
$define_word = "DATAWIDTH".
$define_value = 2

The expression (without the new line):
$row =~ s/([0-9]+)\s*\*\s*$define_word\s*\*\s*([0-9]+)
/$define_value*$1*$2/gi;

just give me the string "...2*3*4...". How can I make this "...24..."

best regards,
Andreas Lundgren

$row =~ s/([0-9]+)\s*\*\s*$define_word\s*\*\s*([0-9]+)
/eval( $define_value*$1*$2 )/gei;

Chris
 
T

Tore Aursand

I want to translate a part of a line that looks like:
"...3*DATAWIDTH*4..." to "...24...".

I have the variables
$define_word = "DATAWIDTH".
$define_value = 2

The expression (without the new line):
$row =~ s/([0-9]+)\s*\*\s*$define_word\s*\*\s*([0-9]+)
/$define_value*$1*$2/gi;

just give me the string "...2*3*4...". How can I make this "...24..."

Hmm. What about the following?

my $string = '...3*DATAWIDTH*4...';
my $word = 'DATAWIDTH';
my $value = 2;

$string = s{(\d+)\*$word\*(\d+)}{ $1 * $value * $2 }eg;
print $string . "\n";
 
D

David K. Wall

Andreas said:
I want to translate a part of a line that looks like:
"...3*DATAWIDTH*4..." to "...24...".

I have the variables
$define_word = "DATAWIDTH".
$define_value = 2

The expression (without the new line):
$row =~ s/([0-9]+)\s*\*\s*$define_word\s*\*\s*([0-9]+)
/$define_value*$1*$2/gi;

This is wrapped, isn't it?
just give me the string "...2*3*4...". How can I make this "...24..."

See 'perldoc perlop', s/PATTERN/REPLACEMENT/egimosx. You want the 'e'
option.

You can shorten the regex a little by using \d instead of [0-9].

$row =~ s/(\d+)\s*\*\s*$define_word\s*\*\s*(\d+)/$define_value*$1*$2/egi;
 
B

Ben Morrow

David K. Wall said:
You can shorten the regex a little by using \d instead of [0-9].

They are different.

perl -le'print "\x{660}" =~ /\d/' # U+0660 is ARABIC-INDIC DIGIT ZERO
1

Perl is inconsistent, however:

perl -le'print 0+"\x{662}"'
0

:)
$row =~ s/(\d+)\s*\*\s*$define_word\s*\*\s*(\d+)/$define_value*$1*$2/egi;

I would also strongly recommend using /x with all those *\* in there:

$row =~ s/(\d+) \s* \* \s* $define_word \s* \* \s* (\d+)
/$define_value * $1 * $2/egix;

(and now you can wrap it with impunity).

Ben
 

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,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top