Problem with Range Operator sequence number

Discussion in 'Perl Misc' started by Paul Porcelli, Jul 14, 2004.

  1. I have the following code(excerpt) which grabs some lines from a
    syslog
    file and adds any found in the range to an array.

    @lines=();@vvlines=();
    $t = new Net::Telnet (Timeout => 30, Prompt => '/<\d+\>/');
    $t->open($t3);
    $t->login($username, $passwd);
    @lines=$t->cmd("tail -1000 syslog");
    $t->close;
    foreach (@lines) {
    push(@vvlines,$_) if /Volume vol[1-2] verification started/ ..
    /Volume vol[1-2] verification ended/;
    }

    This piece of code is part of a foreach loop which iterates two times.
    I have a problem with the following situation:

    Pass 1 of the foreach finds the left side of the range but never finds
    the right side. This means that the range operator is still true.

    Pass 2 of the foreach now pushes all the lines onto the vvlines array
    as the range operator is still true. This is not desired. I only want
    to push the lines after the left side is matched.

    How do I reset the range operator to false between each iteration of
    the foreach loop ?

    Many thanks.

    Paul Porcelli
     
    Paul Porcelli, Jul 14, 2004
    #1
    1. Advertising

  2. [nonexistent comp.lang.perl newsgroup removed]

    On 14 Jul 2004 08:41:49 -0700, Paul Porcelli wrote:
    > I have the following code(excerpt) which grabs some lines from a
    > syslog file and adds any found in the range to an array.
    >
    > @lines=();@vvlines=();
    > $t = new Net::Telnet (Timeout => 30, Prompt => '/<\d+\>/');
    > $t->open($t3);
    > $t->login($username, $passwd);
    > @lines=$t->cmd("tail -1000 syslog");
    > $t->close;
    > foreach (@lines) {
    > push(@vvlines,$_) if /Volume vol[1-2] verification started/ ..
    > /Volume vol[1-2] verification ended/;
    > }


    Not sure what you're expecting the [1-2] to do here. Why not use [12]?
    Or are you expecting to match the literal string "1-2"?

    Oh, and make sure you "use strict".
     
    John J. Trammell, Jul 14, 2004
    #2
    1. Advertising

  3. Paul Porcelli

    Anno Siegel Guest

    Paul Porcelli <> wrote in comp.lang.perl.misc:
    > I have the following code(excerpt) which grabs some lines from a
    > syslog
    > file and adds any found in the range to an array.
    >
    > @lines=();@vvlines=();
    > $t = new Net::Telnet (Timeout => 30, Prompt => '/<\d+\>/');
    > $t->open($t3);
    > $t->login($username, $passwd);
    > @lines=$t->cmd("tail -1000 syslog");
    > $t->close;
    > foreach (@lines) {
    > push(@vvlines,$_) if /Volume vol[1-2] verification started/ ..
    > /Volume vol[1-2] verification ended/;
    > }
    >
    > This piece of code is part of a foreach loop which iterates two times.
    > I have a problem with the following situation:
    >
    > Pass 1 of the foreach finds the left side of the range but never finds
    > the right side. This means that the range operator is still true.
    >
    > Pass 2 of the foreach now pushes all the lines onto the vvlines array
    > as the range operator is still true. This is not desired. I only want
    > to push the lines after the left side is matched.
    >
    > How do I reset the range operator to false between each iteration of
    > the foreach loop ?


    Make sure the second condition becomes true at the end of the file.
    Untested:

    /Volume vol[1-2] verification started/ ..
    ( /Volume vol[1-2] verification ended/ || eof);

    The parentheses around the second operand may be unnecessary.

    Anno
     
    Anno Siegel, Jul 15, 2004
    #3
  4. Paul Porcelli

    Anno Siegel Guest

    Paul Porcelli <> wrote in comp.lang.perl.misc:
    > I have the following code(excerpt) which grabs some lines from a
    > syslog
    > file and adds any found in the range to an array.
    >
    > @lines=();@vvlines=();
    > $t = new Net::Telnet (Timeout => 30, Prompt => '/<\d+\>/');
    > $t->open($t3);
    > $t->login($username, $passwd);
    > @lines=$t->cmd("tail -1000 syslog");
    > $t->close;
    > foreach (@lines) {
    > push(@vvlines,$_) if /Volume vol[1-2] verification started/ ..
    > /Volume vol[1-2] verification ended/;
    > }
    >
    > This piece of code is part of a foreach loop which iterates two times.
    > I have a problem with the following situation:
    >
    > Pass 1 of the foreach finds the left side of the range but never finds
    > the right side. This means that the range operator is still true.
    >
    > Pass 2 of the foreach now pushes all the lines onto the vvlines array
    > as the range operator is still true. This is not desired. I only want
    > to push the lines after the left side is matched.


    Your question would have been much clearer if yuo had actually *shown*
    the outer loop, together with a set of data that throws ".." out of
    sync.

    The code you posted doesn't show your problem, so you got a few replies
    that don't address your problem.

    > How do I reset the range operator to false between each iteration of
    > the foreach loop ?


    Make sure the second condition becomes true at the end of the file.
    Untested:

    /Volume vol[1-2] verification started/ ..
    ( /Volume vol[1-2] verification ended/ || eof);

    The parentheses around the second operand may be unnecessary.

    Anno
     
    Anno Siegel, Jul 15, 2004
    #4
  5. Purl Gurl <> wrote in message news:<>...
    > Paul Porcelli wrote:
    >
    > > /Volume vol[1-2] verification started/ .. /Volume vol[1-2] verification ended/;

    >
    > > Pass 1 of the foreach finds the left side of the range but never finds
    > > the right side. This means that the range operator is still true.

    >
    > > How do I reset the range operator to false between each iteration of
    > > the foreach loop ?

    >
    > /Volume vol[1-2] verification started/ ... /Volume vol[1-2] verification ended/;
    >
    >
    > Try using three periods instead of two periods.
    >
    > You do not provide enough precise information about your
    > data to fully assess what you are doing.
    >
    >
    > Purl Gurl



    First pass @lines has the following

    Blah Blah Blah
    Volume vol1 verification started
    Useful Data
    Useful Data

    So, it finds the start of the range but not the end.
    That is OK as I want all that data
    =======================

    Second pass , @lines has info from another syslog file i.e.
    Blah Blah Blah
    Volume vol1 verification started
    Useful Data
    Useful Data
    Volume vol1 verification ended

    What I would like to happen here, is for that first value(Blah etc) to
    be ignored. i.e. I want the range check to start from scratch again.
    It doesn't though. It pulls out that first line(Blah etc) as well.
    As I understand it, that is because the end of the range was never
    detected during the first pass. So what I need is some way for the
    range operator
    to think the end of the range was found at the end of the first pass.

    HTH
    Paul
     
    Paul Porcelli, Jul 15, 2004
    #5
  6. Purl Gurl <> wrote in message news:<>...
    > Paul Porcelli wrote:
    >
    > > /Volume vol[1-2] verification started/ .. /Volume vol[1-2] verification ended/;

    >
    > > Pass 1 of the foreach finds the left side of the range but never finds
    > > the right side. This means that the range operator is still true.

    >
    > > How do I reset the range operator to false between each iteration of
    > > the foreach loop ?

    >
    > /Volume vol[1-2] verification started/ ... /Volume vol[1-2] verification ended/;
    >
    >
    > Try using three periods instead of two periods.
    >
    > You do not provide enough precise information about your
    > data to fully assess what you are doing.
    >
    >
    > Purl Gurl



    First pass @lines has the following

    Blah Blah Blah
    Volume vol1 verification started
    Useful Data
    Useful Data

    So, it finds the start of the range but not the end.
    That is OK as I want all that data
    =======================

    Second pass , @lines has info from another syslog file i.e.
    Blah Blah Blah
    Volume vol1 verification started
    Useful Data
    Useful Data
    Volume vol1 verification ended

    What I would like to happen here, is for that first value(Blah etc) to
    be ignored. i.e. I want the range check to start from scratch again.
    It doesn't though. It pulls out that first line(Blah etc) as well.
    As I understand it, that is because the end of the range was never
    detected during the first pass. So what I need is some way for the
    range operator
    to think the end of the range was found at the end of the first pass.

    HTH
    Paul
     
    Paul Porcelli, Jul 15, 2004
    #6
  7. Paul Porcelli

    Brad Baxter Guest

    On Thu, 15 Jul 2004, Purl Gurl wrote:
    >
    > for (@lines)
    > {
    > if ($_ =~ /Volume vol[12] verification started(.*)Volume vol[12] verification ended/s)
    > { push(@vvlines, $1); }
    > }


    Forgive me for mentioning it, but following are alternative ways of saying
    that:

    for (@lines)
    {
    if (/Volume vol[12] verification started(.*)Volume vol[12] verification ended/s)
    { push(@vvlines, $1); }
    }

    for (@lines)
    {
    push(@vvlines, $1)
    if /Volume vol[12] verification started(.*)Volume vol[12] verification ended/s
    }

    for (@lines)
    {
    /Volume vol[12] verification started(.*)Volume vol[12] verification ended/s
    && push(@vvlines, $1);
    }

    for (@lines)
    {
    /Volume vol[12] verification started(.*)Volume vol[12] verification ended/s
    and push(@vvlines, $1);
    }


    Regards,

    Brad
     
    Brad Baxter, Jul 15, 2004
    #7
  8. Paul Porcelli

    Eric Amick Guest

    On 15 Jul 2004 10:54:39 GMT, -berlin.de (Anno
    Siegel) wrote:

    >Make sure the second condition becomes true at the end of the file.
    >Untested:
    >
    > /Volume vol[1-2] verification started/ ..
    > ( /Volume vol[1-2] verification ended/ || eof);
    >
    >The parentheses around the second operand may be unnecessary.


    They aren't, but this is definitely a time when expecting people to know
    the difference in precedence is a Bad Idea.

    --
    Eric Amick
    Columbia, MD
     
    Eric Amick, Jul 16, 2004
    #8
    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. Paul Porcelli
    Replies:
    1
    Views:
    463
    Chris Charley
    Jul 14, 2004
  2. Paul Porcelli
    Replies:
    3
    Views:
    522
    Eric Amick
    Jul 16, 2004
  3. Replies:
    46
    Views:
    986
    Antoon Pardon
    Jul 25, 2006
  4. stef mientki
    Replies:
    13
    Views:
    666
    stef mientki
    Oct 20, 2007
  5. Paul Porcelli

    Problem with Range Operator sequence number

    Paul Porcelli, Jul 14, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    130
    Sandman
    Jul 15, 2004
Loading...

Share This Page