in reply to unpack - array takes too many bytes

The @twobytes array swallows all remaining bytes of the file, not just the 2 is am seeking.

You can use array slices to prevent the array from swallowing up more than you want:

my $data = "ABCDEFG"; my ($one, @two, @four); (@two[0..1], @four[0..3], $one) = unpack "C2 C4 C", $data; print "@two | @four | $one"; # 65 66 | 67 68 69 70 | 71

But note that assigning to a slice does not clear any other elements that might have been in the array.

Replies are listed 'Best First'.
Re^2: unpack - array takes too many bytes
by jjap (Monk) on Apr 15, 2011 at 04:58 UTC
    Wonderful!

    I guess my misunderstanding was to expected the digit in C2 to limit the assignment to 2 bytes, which it clearly does not.

    Actually replacing C2 by C1 in your code like so:
    (@two[0..1], @four[0..3], $one) = unpack "C1 C4 C", $data; print "@two | @four | $one"; # 65 66 | 67 68 69 70 |
    And warns for uninitialized likely undef. I am really missing the purpose of what the digit does!?
      I am really missing the purpose of what the digit does!?

      Essentially, the unpack just returns a list of values that you have to assign in some meaningful way.

      The digits in the template are just telling unpack how to parse the input data. No provisions are made to assign "sub-lists" to separate arrays, or some such.  In other words, in the example "C2 C4 C" has the same effect as saying "C7".  But note that in other cases, such as "A2 A4 A", where A2 etc. would return a single element, there would a difference to saying "A7".

        The difference is that "C" is a fixed length format (one byte) while "A" is a variable length format so a number after "C" specifies how many Cs while a number after "A" specifies the length of one A.