in reply to Unpack/format

This may be an ugly hack, but it works:
$pad = ' ' x 80; ## make the length the same as your max expected $c = "test test test"; $fmt = "A4 x1 A4 x1 A4 x1 A4"; @c = unpack($fmt, "$c$pad"); print join ("*",@c), "\n"; $d = "test test test "; $fmt = "A4 x1 A4 x1 A4 x1 A4"; @d = unpack($fmt, "$d$pad"); print join ("*",@d), "\n";

--Jim

Update: BTW, I would avoid using $a as a variable since it (and $b) has an evil association with sort. Check the link for details.

Replies are listed 'Best First'.
Re: Unpack/format
by Reverend Phil (Pilgrim) on Feb 13, 2002 at 17:17 UTC
    Thanks Jim. That'd definitely kill the 'x' issue, and I'll probably put that there. However, if I'm going to handle this, I'd rather trap short entries and yell at the customer. Currently, I yell at them for a variety of reasons via email within the script. I'd like to junk any file that has short records like this, and notify the client. I guess I'm looking for the prettiest way that someone can think of, and wondering if there's something out there that quickly calculates the expected length given a pack/unpack style format string.

    As for the $a/$b - thanks for kicking me =) I know this, and generally do not use such nefarious variables. They show up only when I'm hitting the interpreter directly on my stinky Win32 box in order to test some snippet (like this one). =)