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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi all, I have a problem I just cannot figure out. I'm reading an input line by line and matching stuff to pass into a key. The input data is as follows

dnsname1: static-route 1.1.1.1/30 next-hop 2.2.2.2 preferen +ce 200 bfd-enable tag 685 dnsname1- service-name "XYZ12345-s12345" dnsname2: static-route 3.3.3.3/30 next-hop 4.4.4.4 bfd-enab +le tag 635 dnsname2- service-name "XYZ67891-s67890"

I initially matched just on the static-route line to print it out with the dns name, but actually it would be good to also print out what service it belongs to, which appears on the line below. How would I match on both lines and pass the service-name to a variable as well that I could feed into the same key? The code is riddled with perltidy errors as I've been playing, but this is how far I got.

if (@grep) { my $hop; my $static; my $tag; my $vpna; my $dns; print GREEN "\nFound some static routes...\n", RESET; foreach my $line (@grep) { if $line ( =~ m/^(\S+):\s+static-route (\S+) next-hop (\S+)( pre +ference \d+|)( bfd-enable|) tag (\d+)/ ){ my ( $dns, $static, $hop, $tag ) = ( $1, $2, $3, $6 ); } if $line ( =~ m/^(\S+)-\s+service-name (\S+)/ ){ my ($vpna) = ( $2 ); } $results{$dns}{$vpna}{$static}{$hop} = $tag; } print Dumper \%results; for my $box ( keys %results ) { print BOLD "\n$box\n", RESET; for my $static ( keys %{ $results{$box} } ) { for my $hop ( keys %{ $results{$box}{$static} } ) { print "$static\t next-hop $hop\t"; print "tag $results{$box}{$static}{$hop}\n"; print "\n"; } } } } else { print "No static routes exist for $ip\n"; } }

In reply to Store multiple lines by bartrad

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 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?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-19 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found