First, since you check the string with an initial match, there's no need to s/// it separately to trim the whitespace: just capture the part you're interested in and use it directly. Also, all those lazy quantifiers should be greedy: that which follows your plus quantifiers can never be matched by that which is quantified (ie \s can never match \S and vice versa), so greedy vs lazy does not change the match semantics. And greedy is both more efficient and makes for less clutter. I'd add an /x for good measure.

elsif ( /^ \s+ ( \S{13} \s+ \S+ \s+ \S.* )/x ) { my @fields = unpack "a21 a9 a9 a2 a13 a8 a9 a9 a4 a5 a6", $1; # ... }

What follows in your case has a lot of repetition: the print, the formatting whitespace, and the reference to @fields is duplicated over and over. You can do better than that:

elsif ( /^ \s+ ( \S{13} \s+ \S+ \s+ \S.* )/x ) { my @field = qw( PIIN FSCM N/A U/I UNIT PRICE AWD DT QTY OPT DT FOB REP TYPE ); my %value; @value{ @field } = unpack "a21 a9 a9 a2 a13 a8 a9 a9 a4 a5 a6", $1 +; printf " %-10s = %s\n", $_, $value{ $_ } for @field; print "\n"; }

Makeshifts last the longest.


In reply to Re: A more elegant use of unpack by Aristotle
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.