Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Store multiple lines

by hippo (Bishop)
on Feb 09, 2018 at 12:42 UTC ( [id://1208820]=note: print w/replies, xml ) Need Help??


in reply to Store multiple lines

This might be what you're after, but it's probably fragile and definitely still horribly messy. I wouldn't start from where you are, in all honesty.

use strict; use warnings; my @grep = <DATA>; my $ip = 'whatever'; if (@grep) { my $hop; my $static; my $tag; my $vpna; my $dns; my %results; print "\nFound some static routes...\n"; while (my $line = shift @grep) { if ($line =~ m/^(\S+):\s+static-route (\S+) next-hop (\S+)( preference +\d+|)( bfd-enable|) tag (\d+)/ ) { ($dns, $static, $hop, $tag) = ($1, $2, $3, $6); $line = shift @grep; $line =~ m/^(\S+)-\s+service-name (\S+)/; $vpna = $2; $results{$dns}{$vpna}{$static}{$hop} = $tag; } } #print Dumper \%results; for my $box (keys %results) { print "\n$box\n"; for my $service (keys %{$results{$box}}) { for my $static (keys %{$results{$box}{$service}}) { for my $hop (keys %{$results{$box}{$service}{$static}} +) { print "$static\t next-hop $hop\t"; print "tag $results{$box}{$service}{$static}{$hop}, se +rvice $service\n"; print "\n"; } } } } } else { print "No static routes exist for $ip\n"; } __DATA__ 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"

Replies are listed 'Best First'.
Re^2: Store multiple lines
by bartrad (Beadle) on Feb 09, 2018 at 13:27 UTC

    Thanks for your help! As you can probably tell, I'm learning as I go and certainly appreciating the more than one way to do things in Perl. I think what I'm more interested in is actually the best way to do it. Appreciate you've called out the method is messy, and I have to agree. Could I ask possibly how you would do it please?

      It might be simplified depending on relationships between the data columns. For example, if the data was arranged as a table like this

      dnsname   static-route  next-hop  preference  bfd-enable  tag  service-name 
      dnsname1  1.1.1.1/30    2.2.2.2   200         Y           685  XYZ12345-s12345
      dnsname2  3.3.3.3/30    4.4.4.4               Y           635  XYZ67891-s67890
      

      Would the first 2 (or 3) columns uniquely define a single row in the table ?

      poj

      Good question! It rather depends on the properties of your data and how you want it structured. I guess that each "service-name" is unique? You could then have a much flatter data structure if you just keyed on that. eg:

      • results
        • service name
          • dns
          • static
          • next hop
          • tag

      But maybe that's no use to you depending on how you would want to use the data later on. Once you know what most useful form the data should take you can go about constructing it (and that might even involve some pre-processing).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-04-25 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found