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

Re: Formatting an array

by roboticus (Chancellor)
on Feb 08, 2018 at 02:58 UTC ( #1208675=note: print w/replies, xml ) Need Help??


in reply to Formatting an array

bartrad:

If the fields are always the same length, then substr and unpack can be good choices. Alternatively, if there are consistent delimiters, then split could be a good choice.

As an example:

$ cat t.pl use strict; use warnings; for my $line (<DATA>) { # Unpack can pull out multiple fields from a string and also trim +and/or # perform some simple conversions. # You do, however, have to count out your field sizes. my @u_flds = unpack "A52A24A*", $line; print "UNPACK: router: <$u_flds[0]>\n"; print "\ttype and IP: <$u_flds[1]>\n"; # substr can be useful, but you it's a little cumbersome for many +fields. # For only a couple, though, it's not too bad. my $router = substr($line, 0, 52); my $route_and_IP = substr($line,52,24); print "SUBSTR: router: <$router>\n"; print "\ttype and IP: <$route_and_IP>\n"; # Split is good if you have consistent delimiters. If your delimi +ter occurs # in one or more of your fields, though, you may have to glue some + together. my @s_flds = split /\s+/, $line; print "SPLIT: router: <$s_flds[0]>\n"; print "\ttype and IP: <$s_flds[1] $s_flds[2]>\n"; print "\n"; } __DATA__ /data/alcatel_ethernet_7750/routername1- static-route 1.1.1 +.1/30 next-hop 2.2.2.2 bfd-enable tag 624 /data/alcatel_ethernet_7750/routername2- static-route 1.1.1 +.3/30 next-hop 3.3.3.3 bfd-enable tag 624 /data/alcatel_ethernet_7750/routername3- static-route 1.1.1 +.2/30 next-hop 4.4.4.4 bfd-enable tag 628 $ perl t.pl UNPACK: router: </data/alcatel_ethernet_7750/routername1-> type and IP: <static-route 1.1.1.1/30> SUBSTR: router: </data/alcatel_ethernet_7750/routername1- > type and IP: <static-route 1.1.1.1/30 > SPLIT: router: </data/alcatel_ethernet_7750/routername1-> type and IP: <static-route 1.1.1.1/30> UNPACK: router: </data/alcatel_ethernet_7750/routername2-> type and IP: <static-route 1.1.1.3/30> SUBSTR: router: </data/alcatel_ethernet_7750/routername2- > type and IP: <static-route 1.1.1.3/30 > SPLIT: router: </data/alcatel_ethernet_7750/routername2-> type and IP: <static-route 1.1.1.3/30> UNPACK: router: </data/alcatel_ethernet_7750/routername3-> type and IP: <static-route 1.1.1.2/30> SUBSTR: router: </data/alcatel_ethernet_7750/routername3- > type and IP: <static-route 1.1.1.2/30 > SPLIT: router: </data/alcatel_ethernet_7750/routername3-> type and IP: <static-route 1.1.1.2/30>

While the sample data you provided looks pretty regular, I expect that your field sizes are going to be variable (especially as one is an IP), so I expect that split may be a good choice for you.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Formatting an array
by bartrad (Sexton) on Feb 08, 2018 at 12:07 UTC

    Thanks for the reply, I like this. How would I get rid of the leading path to routernamex as I don't need it?

      bartrad:

      Assuming that all your path components are separated by "/", then you would simply use split again, and take the final component(s):

      my @router_path_components = split qr{/}, $s_flds[0]; my $last_chunk = $router_path_components[-1];

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Thank you

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208675]
help
Chatterbox?
Jar. Jar!...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-05-28 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?