regular expression error

R

Roman Kaganovich

Hello ,

I wrote a program based an example from "Perl Cookbook".


I run it at file test that contain this text:

blah blah (22.34 215.645)
something (1978 404)
opla 1234

When I use code:

#!/usr/bin/perl -w

my $x=$ARGV[1];
open FILE,$ARGV[0] or die "$!";
while (<FILE>){
chomp;
s/\((\d+)/$x * $1/e;
$x=$ARGV[1];
print "$_\n";
}

I got no errors.
But if I modify a code to:

#!/usr/bin/perl -w

my $x=$ARGV[1];
my $y=$ARGV[2];
open FILE,$ARGV[0] or die "$!";
while (<FILE>){
chomp;
s/\((\d+\.\d+|\d+?)(\s+?)(\d+\.\d+|\d+?)/$x * $1 $y * $3/ex;
$x=$ARGV[1];
$y=$ARGV[2];

print "$_\n";
}


I got next output:
./col.pl test 10 2
Scalar found where operator expected at ./col.pl line 10, near "$1 $y"
(Missing operator before $y?)
syntax error at ./col.pl line 10, near "$1 $y"
Execution of ./col.pl aborted due to compilation errors.
 
P

Paul Lalli

#!/usr/bin/perl -w

my $x=$ARGV[1];
my $y=$ARGV[2];
open FILE,$ARGV[0] or die "$!";
while (<FILE>){
chomp;
s/\((\d+\.\d+|\d+?)(\s+?)(\d+\.\d+|\d+?)/$x * $1 $y * $3/ex; ^^^^^^^^^^^^^^^

$x=$ARGV[1];
$y=$ARGV[2];

print "$_\n";
}


I got next output:
./col.pl test 10 2
Scalar found where operator expected at ./col.pl line 10, near "$1 $y"
(Missing operator before $y?)
syntax error at ./col.pl line 10, near "$1 $y" ^^^^^
Execution of ./col.pl aborted due to compilation errors.


Perl is telling you exactly what's wrong. What are you thinking your code
is going to do? That underlined portion in your regexp is a syntax error,
as you provided the /e modifier and:
$x * $1 $y * $3
is not a valid expression. It looks like *maybe* you're trying to print
the results of both operations to standard output? In that case, you need
to tell Perl to concatenate the two values:

($x * $1) . ' ' . ($y * $3)

Or are you trying to do something else?

Paul Lalli
 
R

Richard Morse

Roman Kaganovich said:
s/\((\d+\.\d+|\d+?)(\s+?)(\d+\.\d+|\d+?)/$x * $1 $y * $3/ex;

Scalar found where operator expected at ./col.pl line 10, near "$1 $y"
(Missing operator before $y?)
syntax error at ./col.pl line 10, near "$1 $y"

In your substitute, you are using the 'e' flag. This causes the
substituted value to be treated as an expression. If you run the
following program:

#!/usr/bin/perl
use strict;
use warnings;

my $x = 5;
$1 = 6;
my $y = 7;
$3 = 8;

my $z = $x * $1 $y * $3;

print $z;

__END__

you get the same error. This is because your haven't told perl how to
combine $1 and $y -- perhaps you meant:

($x * $1) * ($y * $3)
or
($x * $1) + ($y * $3)
or
($x * $1) . ($y * $3)
or even
($x * $1) x ($y * $3)

How is perl to know?

You probably (based on the context, and my guess) want

($x * $1) . ' ' . ($y * $3)

HTH,
Ricky
 
G

grom

Thanks a lot.
It's work.

Richard said:
In your substitute, you are using the 'e' flag. This causes the
substituted value to be treated as an expression. If you run the
following program:

#!/usr/bin/perl
use strict;
use warnings;

my $x = 5;
$1 = 6;
my $y = 7;
$3 = 8;

my $z = $x * $1 $y * $3;

print $z;

__END__

you get the same error. This is because your haven't told perl how to
combine $1 and $y -- perhaps you meant:

($x * $1) * ($y * $3)
or
($x * $1) + ($y * $3)
or
($x * $1) . ($y * $3)
or even
($x * $1) x ($y * $3)

How is perl to know?

You probably (based on the context, and my guess) want

($x * $1) . ' ' . ($y * $3)

HTH,
Ricky
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top