SamCG has asked for the wisdom of the Perl Monks concerning the following question:

Good monks,
I am having an issue with unpacking some data I receive from a vendor as a fixed-length data file (without any delimiters). I am using a WinXP machine w/ ActiveState perl 5.8. Basically, I'm happy with unpacking everything as text (though there is numeric data; all I want to do is print it out -- can I unpack numeric data as text?).
if (/GAA\d{8}4VD\d{6}/i){ ($jnk,$acctid,$cusip,$jnk2,$symbol,$jnk3,$trantype,$buysell,$jnk4,$tra +dedate,$settledate,$timeformat,$src,$pershingref,$batchcode,$oppacct, +$mktcode,$blottercode,$cancelcode,$corrcode,$mktlimit,$jnk5,$quantity +,$qsign,$price,$jnk6,$prcsign,$pricecurrency, $netamt, $principal,$in +terest, $comm) = unpack ($_, "A11A10A9A17A16A6A1A1A18A8A8A7A2A6A6A10A1A1A1A1A1A4A18A1A1 +8A5A1A3A19A19A19A19");
The relevant data looks similar to:
GAA000000144VD0000011G76893111 G76893111 RBSPF 000 T +S 03002004071320040713200407160154628 #45678*GLOS 4VD000001145 + 000000001000000000-000000015610000000 +USD00000000020000140 +0-000000000200001400-000000000000000000 000000000000000000 0000000000 +00000000 000000000000000000 000000000000000000 000000000000000000 000 +000000000000000 000000000000000000 000000000000000000 000000000000000 +000 000000000000000000 000000000000000000 000000000000000000 00000000 +00000000000 000000000000000000000000000000000000 000000000000000000 + 10 000000000000000000 0000 +00023640000000+ + X
I was under the impression I only need to define the template up to the point where I want data. I get an "Invalid type in unpack: 'G'" error. I get this even if I limit my unpacking to the first field, i.e.,
if(/GAA\d{8}4VD\d{6}/i){ ($jnk) = unpack ($_, "a11");}
I've seen this error referenced in a similar fashion in various places on the internet (usually with type 'Q'), but I haven't seen an indication of the solution.

One note: I'm not sure what the source of the data is (I have a well-defined layout, but I don't know what type of machine/process is producing my file). I've read something about different encoding possibly affecting unpack, but typically only in relation to numeric data.

Any help appreciated,

SamCGard

"For thousands more years the mighty ships tore across the empty wastes of space and finally dived screaming on to the first planet they came across - which happened to be the Earth - where due to a terrible miscalculation of scale the entire battle fleet was accidentally swallowed by a small dog." - HHG

Replies are listed 'Best First'.
Re: Invalid Type -- Unpack
by davido (Cardinal) on Sep 01, 2004 at 18:51 UTC

    unpack works better when the format template string comes first, and the data being unpacked comes as the second argument. In fact, it doesn't work at all the other way around. ;)

    eg...

    my(@stuff) = unpack "A9", $packed;

    not...

    my(@stuff) = unpack $packed, "A9"; # Wrong!

    Dave

      Ah, just one more reminder to keep me humble <laughing at myself> Thanks, Dave!