Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

REGexp match query

by Anonymous Monk
on Oct 09, 2015 at 13:24 UTC ( [id://1144299]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to match this output.

Queue counters: Queued packets Trans packets Dropped pak 0 258520344 520344 0 1 11006822 1006822 0 2 832114 0832114 0 3 8294677 8294677 0 4 5416057 5416057 0
I want to check , whether Queue counters: is 0 and there are digits for both Queued packets and Trans packets and Dropped Pak is 0. Similarly for other Queue counters till 4
$rh->cmd('show class-of-service interface et-0/3/0 com +prehensive | find " Queue counters:"');--> this displays the above ou +tput, internal command my $output = $rh->get_response; my @outputlist = split /\n+/, $output; foreach (0..$#outputlist) { if (($outputlist[$_] = /^0/) && ($outputlist[$ +_] = /^0\s(\d+)\s(\d+)/)) { print " match is $outputlist[$_]\n"; } } }

In the above code for sample, i tried to match Q 0 and space and then digit for the Queued packets & Trans packets respectively. Looks to me its not matching. anything missing here

Replies are listed 'Best First'.
Re: REGexp match query
by toolic (Bishop) on Oct 09, 2015 at 13:33 UTC
    $outputlist[$_] = /^0/
    One problem is that you need to use the binding operator =~

    It might be easier to use split:

    use warnings; use strict; while (<DATA>) { chomp; if (s/^\s+//) { my @cols = split; } } __DATA__ Queue counters: Queued packets Trans packets Dropped pak 0 258520344 520344 0 1 11006822 1006822 0 2 832114 0832114 0 3 8294677 8294677 0 4 5416057 5416057 0
      "... easier to use split..."

      Yes. Perhaps even more simplified?

      <DATA>; while (<DATA>) { chomp; my @cols = split; }

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Re: REGexp match query
by Athanasius (Archbishop) on Oct 09, 2015 at 14:03 UTC

    A few additional observations:

    • In the foreach loop there is no need for explicit indices, since these are used only to identify array elements. It’s simpler and clearer to loop directly over the elements themselves: for (@outputlist) { This has the additional benefit that you don’t need to use the binding operator (=~) explicitly, because $_ is “understood”:

      for (@outputlist) { print ... if /.../; }
    • Testing that a string matches /^0/ and then testing that it matches a longer pattern beginnning with /^0.../ is fairly pointless here.

    • The input line:

      " 0 258520344 520344 0"

      will not match any pattern beginning /^0.../ because there is whitespace before the zero digit. You need something like this:

      / ^ \s* 0 \s+ (\d+) \s+ (\d+) \s+ 0 \s $ /x

      Note the quantifiers: * means zero or more, + means one or more. See “Quantifiers” in perlre. (The /x modifier just makes the regex easier to read.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: REGexp match query
by hippo (Bishop) on Oct 09, 2015 at 13:32 UTC
    if (($outputlist[$_] = /^0/) && ($outputlist[$_] = /^0\s(\d+)\s(\d+)/))  {

    That line contains assignments (the = operator) rather than matches (the =~ operator). Try using the latter and see how much closer you get to your desired functionality. See also perlop for more on operators.

      That doesn't help either

        And that answer does not help us helping you. "It does not work" or "it does not help" is a very poor diagnostics and does not give us monks any clue as to how to further help you. Please explain in which respect it does not help you.

        Update: corrected a typo (s/\sup\s/\sus\s/).

Re: REGexp match query
by RichardK (Parson) on Oct 09, 2015 at 15:11 UTC

    Split just isn't going to work if you can have missing columns. If split returns 3 columns how are you going to guess which one is missing?.

    The data you posted looks like it's a fixed length format, so you'd be better using unpack

Re: REGexp match query
by flexvault (Monsignor) on Oct 09, 2015 at 15:42 UTC

    Dear Monks,

    I would use the following to get at the data in each line: (untested)

    my $output = $rh->get_response; $output = join( " ", split " ", $output ); my @outputlist = split /\ /, $output; foreach (0..$#outputlist) { . . .
    Hope this helps!

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-15 18:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found