in reply to Re: why the unpack doesnt split the data to the array ?
in thread why the unpack doesnt split the data to the array ?

No no I would like to get an array @data that would contain 21 items (as it is the count of C's in the template ) So that array would look like this: lets say that $buffer contains these data: f07601089978040001aaaaaa2017102195404724203718262a05051219540400 000199000001040678461666aaaaaaaaaaaaaa00023532200000011820050512 19540472403aaa (this is the hexdump of first 71 bytes read from that binary file) And I want these data to be split to the @data array according to the template I use ->> so I want @data to be like this: (f0, 76, 01, 08 AND SO ON ACCORDING TO THE TEMPLATE) - just unpack the particular data as unsigned chars.. But I probably get it wrong :) I'm thinking about that $buffer as a buffer in the C language :(
  • Comment on Re^2: why the unpack doesnt split the data to the array ?

Replies are listed 'Best First'.
Re^3: why the unpack doesnt split the data to the array ?
by Corion (Patriarch) on Aug 06, 2008 at 19:14 UTC

    What do you think that the C5 does? You use it, and it returns 5 (byte) numbers. It is equivalent to a byte[5] I guess. Maybe you can describe to us what you intend it to do, then we can tell you what other template you should use. Because your "according to the template" is how Perl already behaves. But that does not seem to be what you want.

      well as you can from the code snippet at the beginning:
      @data = unpack('C C C C C C C C C C3 C3 C5 C5 C3 C3 C3 C C3 C C12 C20' +, $buffer);
      So I would expect that @data array would contain 21 items...each item should be as long as it is specified in the template so C - means ONE unsigned char , C5 - FIVE unsigned chars and so on... so I would like the data in $buffer to be unpacked into these 21 items (f0, ..., last twenty bytes - since the last template says C20) That is what I want...SPLIT THE DATA into the @data array...
        Oh! I think you want arrays! You didn't mention arrays at all. Pack can't do that, but the following can:
        @data = unpack('C C C C C C C C C A3 A3 A5 A5 A3 A3 A3 C A3 C A12 A20' +, $buffer); $_ = [ unpack('C*', $_) ] for @data[9, 10, 11, 12, 13, 14, 15, 17, 19, + 20];

        Update: Forgot to specify index. Fixed.

Re^3: why the unpack doesnt split the data to the array ?
by ikegami (Patriarch) on Aug 06, 2008 at 19:21 UTC

    so I want @data to be like this: (f0, 76, 01, 08 AND SO ON

    Hex strings?

    just unpack the particular data as unsigned chars

    There is enough data for 71 chars.