Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found