Any hints on why that happens?

Because that is how array assignment works in Perl. The array expands to accommodate as many items as you assign to it. This is usually seen as a good thing as you'll discover as your experience of Perl grows.

But also, think about how could perl know you only want two values to be assigned to the first array and four to the second? Perl doesn't understand the English in your variable names :)

have over 50 variables of varying size in that header. Am I on a valid approach by appending them in the above statement? That will end up being one looong statement...

Simply put, No. That would not be convenient for you at all. Even if you used the array slice assignment trick: it would be very messy to read & write; difficult to maintain; and very easy to get wrong.

The simplest thing is to assign all fields to a single array and define constant names to access them:

use constant { NAME => 0, ONBYTE => 1, TWOBYTE => 2, FOURBYTE => 3, TWOBYTE2 => 4, TRAILING => 5, }; my @fields = unpack( "Z16 C1 C2 C4 C1 x400 C*", $data ); print $fields[ NAME ]; print $fields[ FOURBYTE ]; print for @fields[ TRAILING .. $#fields ];

Hopefully with better names :)

But realise, that if you want to unpack two bytes as a single entity, the format C2 will not work. It will return 2 single bytes. You probably want something more like:

my @fields = unpack( "Z16 C S L C x400 C*", $data );

That is:1 unsigned byte, 1 unsigned short, 1 unsigned long, another unsigned byte; skip 400 bytes and the unpack what's left as bytes.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: unpack - array takes too many bytes by BrowserUk
in thread unpack - array takes too many bytes by jjap

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.