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.


In reply to Re: Formatting an array by roboticus
in thread Formatting an array by bartrad

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.