regular expression error

Discussion in 'Perl Misc' started by Roman Kaganovich, Apr 26, 2004.

  1. 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.
    Roman Kaganovich, Apr 26, 2004
    #1
    1. Advertising

  2. Roman Kaganovich

    Paul Lalli Guest

    On Mon, 26 Apr 2004, Roman Kaganovich wrote:

    > #!/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
    Paul Lalli, Apr 26, 2004
    #2
    1. Advertising

  3. In article <>,
    Roman Kaganovich <> wrote:

    > 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

    --
    Pukku
    Richard Morse, Apr 26, 2004
    #3
  4. Roman Kaganovich

    grom Guest

    Thanks a lot.
    It's work.

    Richard Morse wrote:
    > In article <>,
    > Roman Kaganovich <> wrote:
    >
    >
    >> 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
    >
    grom, May 2, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,290
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    846
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,955
    vforvikash
    Dec 29, 2008
  4. Lance Hoffmeyer

    regular expression error ?

    Lance Hoffmeyer, May 15, 2006, in forum: Python
    Replies:
    1
    Views:
    320
    Bruno Desthuilliers
    May 16, 2006
  5. monkeyjunkie

    Regular Expression - Show error if string is matched?

    monkeyjunkie, Nov 10, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    183
    Robert Koritnik
    Nov 11, 2004
Loading...

Share This Page