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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
See if this extendable (declarative) coding style works for you:
use strict; use warnings; my %details; my @rules=( "Hardware" => sub{$details{HARDWARE_SEEN} = 1 }, 'cisco Nexus (\S+)' => sub{return 0 unless $details{HARDWARE_SEEN}; $details{Chassis} = $_[0]; }, 'Intel.+?(\d+)\s*(\w{2}) of memory' => sub{ return 0 unless $details{HARDWARE_SEEN} +; $details{"Main Memory"} = $_[0]; $details{"Main Memory Unit"} = $_[1]; }, ); while (<DATA>){ for (my $i=0; $i < $#rules; $i+=2){ my $regex=qr|$rules[$i]|; my $action = $rules[$i+1]; next unless m/$regex/i; last if $action->($1,$2,$3); } } for (sort keys %details){ print "$_\t = $details{$_}\n"; } __DATA__ # Sample output # Hardware # cisco Nexus 3048 Chassis ("48x1GE + 4x10G Supervisor") # Intel(R) Celeron(R) CPU P450 with 3981680 kB of memory.
Output:
Chassis = 3048 HARDWARE_SEEN = 1 Main Memory = 3981680 Main Memory Unit = kB
Feel free to post questions about the code here.

                "From there to here, from here to there, funny things are everywhere." -- Dr. Seuss


In reply to Re: Parse variables from @output by NetWallah
in thread Parse variables from @output by Cisco_Dave

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2021-10-20 23:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (82 votes). Check out past polls.

    Notices?