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

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!utcsri!torn!nott!bnrgate!bcars6a8!bcars68a!jsparkes
  3. From: jsparkes@bcars68a.bnr.ca (Jeff Sparkes)
  4. Subject: Re: unpack and shift idiom?
  5. Message-ID: <1992Dec18.185143.17096@bcars6a8.bnr.ca>
  6. Sender: usenet@bcars6a8.bnr.ca (Use Net)
  7. Nntp-Posting-Host: bcars68a
  8. Organization: Bell-Northern Research Ltd., Ottawa
  9. References: <1992Dec16.180058.15351@bcars6a8.bnr.ca>
  10. Date: Fri, 18 Dec 1992 18:51:43 GMT
  11. Lines: 39
  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. >
  22. >I'm concerned by the need to use a constant as the arg to substr, especially
  23. >since I calculated it wrong the first time, and it took me a while to find.
  24. >This gets especially hairy when I pull strings out of the data stream, since
  25. >I don't know how many bytes to skip after unpacking A strings because some
  26. >stripping occurs.
  27. >
  28. >Please tell me I'm missing something!
  29.  
  30. Well, one thing that I'm missing is that unpack works with fixed size
  31. structures, and expects strings to be fixed length.  (I thought the number
  32. was how many strings to read, not the legnth of the string.  I should learn
  33. to read documenation properly!) Anyway, I was dealing with structures like:
  34.     struct junk {
  35.         long one, two;
  36.         short three, four;
  37.         char *s1;
  38.         char *s2;
  39.     }
  40. which it seems perl it not well suited for.
  41.  
  42. One wish though:  it would be great if unpack would actually remove the
  43. bytes from the front of the string, or at least provide some way to find
  44. out how many bytes were used in the unpacking.
  45.  
  46. Oh well, live and learn.
  47.  
  48. --
  49. --
  50. Jeff Sparkes jsparkes@bnr.ca    Bell-Northern Research, Ottawa (613)765-2503
  51. You know, a muffin can be *very* filling.
  52.