home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / perl / 7545 < prev    next >
Encoding:
Text File  |  1992-12-21  |  1.4 KB  |  39 lines

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