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:

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.