Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Iterating and pattern matching on input file

by roboticus (Chancellor)
on Jul 31, 2018 at 20:06 UTC ( [id://1219596]=note: print w/replies, xml ) Need Help??


in reply to Iterating and pattern matching on input file

bartrad:

Take a look at what the code in your while loop would do for each line of code:

  • Checks: Running 'show port | match "Up    Yes" | count'
    OK, it matches /show port/, so it would skip rest of the body of the while loop.
  • Count: 23 lines
    It doesn't match /show port/, so it goes to the next line in the loop.
    It matches /Count:\s+(\d+)/, so it would add the value to $checks{port}{$stage}
    It doesn't match /show router interface/, so it goes to the next line in the loop.
    It matches /Count:\s+(\d+)/, so it would add place the value to in $checks{l3}{$stage}
    There's nothing else in the loop to do...
  • Checks: Running 'show router interface | match "Up " | count'
    It doesn't match /show port/, so it goes to the next line in the loop.
    It doesn't match /Count:\s+(\d+)/, so it ignores the body of the if statement.
    It matches /show router interface/, so it would skip the rest of the body of the while loop.
  • Count: 4 lines
    It doesn't match /show port/, so it goes to the next line in the loop.
    It matches /Count:\s+(\d+)/, so it would add the value to $checks{port}{$stage}
    It doesn't match /show router interface/, so it goes to the next line in the loop.
    It matches /Count:\s+(\d+)/, so it would add place the value to in $checks{l3}{$stage}
    There's nothing else in the loop to do...

So it looks like you're expecting 'next' to mean "read the next line" instead of "ignore the rest of the loop".

I expect you probably want something like:

while (my $line = <INPUT>) { my item = "UNKNOWN"; if ($line =~ /show port/) { $item = "port"; } if ($line =~ /show router interface/) { $item = "l3"; } if ($line =~ /Count:\s+(\d+)/) { $checks{$item}{$stage} = $1; } }

Update: Pressed "create" just a bit too soon!

Update: As AnomalousMonk mentions, it's not adding the count value.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Iterating and pattern matching on input file
by AnomalousMonk (Archbishop) on Jul 31, 2018 at 20:50 UTC
    It matches /Count:\s+(\d+)/, so it would add the value to ... [emphasis added]

    I agree with your analysis, except I would say "It matches /Count:\s+(\d+)/, so it would assign the value to ... (overwriting the previous value)." (The phrase "add the value to" suggests something like the  += operation (update: but the OPed code does an assign).)


    Give a man a fish:  <%-{-{-{-<

Re^2: Iterating and pattern matching on input file
by bartrad (Beadle) on Jul 31, 2018 at 20:41 UTC

    Thanks for your response. Yeah, that's exactly what I want - "next line", rather than "next". I've updated the code but doesn't seem to be working?

    my $item = "UNKNOWN"; if ( $line =~ m/show port/ ) { $item = + "port"; } if ( $line =~ m/show router interface/ ) { $item = + "l3"; } if ( $line =~ m/Count:\s+(\d+)/ ) { $checks{$item}{$stage} = + $1; }
    $VAR1 = { 'subs' => { 'post' => '2', 'pre' => '2' }, 'bfd' => { 'post' => 0, 'pre' => 0 }, 'ospf' => { 'post' => '2', 'pre' => '2' }, 'UNKNOWN' => { 'post' => '28615', 'pre' => '28615' }, 'ldp' => { 'post' => '14', 'pre' => '14' } };

    Am I missing something?

      bartrad:

      I can't tell what's wrong as you don't show the input data, nor is there enough code to see what's happening.

      From what you're showing me, I'd guess that there's a spelling error in there and the items you're trying to capture are treated as UNKNOWN.

      You might try showing a bit more of your code and/or your test data.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1219596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-28 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found