Just a personal preference but I would do:
my @field_names = qw|PIIN FSCM N/A U/I UNIT PRICE AWD DT QTY OPT DT FO +B REP TYPE|; my $pack_defn = 'A21 A9 A9 A2 A14 A8 A9 A9 A4 A5 A6'; my %fields; @fields{@field_names} = unpack($pack_defn,$_); foreach (@field_names){ print "\t$_\t\= " , $fields{$_},"\n"; }

My changes keep the field names and the field unpack definitions closer together, puts the values into a hash with the appropriate named keys and removes repeated code for the printing (use formats if you want better alignment in the columns).

It may be important to note that this is not as efficient as the way you are currently doing it.

As wfsp noted your 'a's should be 'A's and you are one character off in the 5th field (counting from one). Additionally your regexp in the elsif line does not match any of your sample data lines.

--
Clayton

In reply to Re: A more elegant use of unpack by clscott
in thread A more elegant use of unpack by maida

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.