Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by kennethk (Abbot)
on Jun 15, 2017 at 14:57 UTC ( #1192866=note: print w/replies, xml ) Need Help??


in reply to pack and unpack multiple arrays with one common repeat prefix

For this particular case, you don't need a rep count because the whole thing is very well behaved.
print join(',', unpack("C/C* v*", $testinput)), "\n";
Is this strictly an educational exercise, or do you have more complex data structures to interrogate? If the data is chunked in a different way, Counting Repetitions solves your trouble with parentheses, but this only works in the context where you get to pick how things are packed.
my $testinput = pack('C/a* a* a*', (pack 'Cvs', 1, 3, -5), (pack 'Cvs', 2, 4, -6) ); print join(',', unpack("C/(Cvs)", $testinput)), "\n";

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

Replies are listed 'Best First'.
Re^2: pack and unpack multiple arrays with one common repeat prefix
by hexcoder (Chaplain) on Jun 15, 2017 at 16:21 UTC
    Thanks for the link! I did not known about the pack tutorial before.

    The context for this problem is a table driven telegram decoder, which assumes, it can split the packed telegram data structure into fields in one unpacking step.

    The other structures have been simple enough but the last extension has been more complex with its prefixed repeat count.

    hexcoder
      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.

        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;
Re^2: pack and unpack multiple arrays with one common repeat prefix
by chacham (Prior) on Jun 15, 2017 at 21:09 UTC

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

    That sounds like a really bad idea. That is using a computer to do it faster, not better.

      If you are using different algorithms in your coding than in your life, one of the two is sub-optimal. If you know a better way to do it, why are you wasting precious seconds you could be sitting under a tree?

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

        That is like saying that a computer should play chess the same way a human does.

        Humans make mistakes, forget, and have limited time to crunch numbers. So, we have methods to help us do things. Computers do not have these shortcomings, and thus can do things that we wish we could do. Also, there are patterns a computer can "see" that an unaided human cannot.

        Computers are not just there to implement human algorithms as if they were macros. They are game changers, and should be used as such. Not that everything you do without a computer should be thrown out. Keep what works best, but make sure you utilize the computer for its strengths, not just its speed.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1192866]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-06-20 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (116 votes). Check out past polls.

    Notices?