home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!usc!news.cerf.net!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Subject: Re: unpack and shift idiom?
- Message-ID: <1992Dec19.022538.2379@netlabs.com>
- Sender: news@netlabs.com
- Nntp-Posting-Host: scalpel.netlabs.com
- Organization: NetLabs, Inc.
- References: <1992Dec16.180058.15351@bcars6a8.bnr.ca>
- Date: Sat, 19 Dec 1992 02:25:38 GMT
- Lines: 26
-
- In article <1992Dec16.180058.15351@bcars6a8.bnr.ca> jsparkes@bcars68a.bnr.ca (Jeff Sparkes) writes:
- : I'm reading binary data from a network stream. I've accumulated the data in
- : a variable called $data, and am taking it apart in chunks. The code
- : looks like:
- : @output = unpack("L4S6", $data);
- : $data = substr($data, 28);
- : @names = unpack("A20", $data);
- : ???
- :
- : I'm concerned by the need to use a constant as the arg to substr, especially
- : since I calculated it wrong the first time, and it took me a while to find.
- : This gets especially hairy when I pull strings out of the data stream, since
- : I don't know how many bytes to skip after unpacking A strings because some
- : stripping occurs.
- :
- : Please tell me I'm missing something!
-
- Perhaps you just need to make unpack do the counting for you.
-
- @output = 0..9; # make sure elements exist
- (@output[0..9], $names, $remainder) = unpack("L4 S6 A20 a*", $data);
-
- A20 does do stripping, as you say, but the unpack will continue unpacking
- after the full 20 bytes, even if what is returned is shorter.
-
- Larry
-