Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: A more elegant use of unpack

by clscott (Friar)
on Sep 03, 2004 at 16:42 UTC ( [id://388339]=note: print w/replies, xml ) Need Help??


in reply to A more elegant use of unpack

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

Replies are listed 'Best First'.
Re^2: A more elegant use of unpack
by mifflin (Curate) on Sep 03, 2004 at 18:32 UTC
    deleted by mifflin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://388339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found