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.
In reply to Re: Formatting an array
by roboticus
in thread Formatting an array
by bartrad
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |