Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: pack and unpack multiple arrays with one common repeat prefix

by kennethk (Abbot)
on Jun 15, 2017 at 19:05 UTC ( [id://1192886]=note: print w/replies, xml ) Need Help??


in reply to Re^2: pack and unpack multiple arrays with one common repeat prefix
in thread pack and unpack multiple arrays with one common repeat prefix

So, for clarity, does the following always hold?
  • byte: number of members in each of the following arrays
  • array of bytes
  • array of unsigned shorts
  • another array of unsigned shorts
Because, from the Perl perspective, that's functionally equivalent to:
  • byte: number of members in array of bytes
  • array of bytes
  • array of unsigned shorts
and thus C/C* v* does everything you need.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

  • Comment on Re^3: pack and unpack multiple arrays with one common repeat prefix
  • Download Code

Replies are listed 'Best First'.
Re^4: pack and unpack multiple arrays with one common repeat prefix
by hexcoder (Curate) on Jun 15, 2017 at 22:06 UTC
    In general, no, it does not hold. For the example I extracted only the part that I had problems with. In real life there is more structure before and after the example structure. I also hoped to learn how flexible and concise the packing templates can be, but of course I need to digest the perlpacktut documentation now (which I did not knew existed). Thanks for good advice.

    Edit: Here is a more general example with three arrays of different sized types. Now it is
  • byte: number of members in each of the following arrays
  • array of bytes
  • array of unsigned shorts
  • array of longs
  • I updated the script and added the one step solution (using the excellent explanation of Eily):

    use strict; use warnings; my $testinput = pack('C/a* a* a*', (pack 'C*', 1, 2), (pack 'v*', 3, 4), (pack 'l*', 5, 6)); print join(',', unpack('C/C* v2 l2', $testinput)), "\n"; # gives "1,2,3,4,5,6" which is ok, # but has the repeat factors for 'v' hardcoded my $repeat = unpack('C', $testinput); print join(',', unpack("C/C* v$repeat l$repeat", $testinput)), "\n"; # gives "1,2,3,4,5,6" which is ok, but uses two steps print join(',', unpack('C/C* @0 CXC /(x[C]) xX /v @0 CXC /((x[C])(x[v] +)) xX /l', $testinput)), "\n"; # gives "1,2,3,4,5,6" uses one step, but is a bit complex 1;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192886]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-23 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found