Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: unpack into arrayrefs?

by salva (Canon)
on Feb 22, 2022 at 16:25 UTC ( #11141552=note: print w/replies, xml ) Need Help??


in reply to unpack into arrayrefs?

I don't think there is a way to do that with unpack in just one pass... but...
my @a = map [unpack "CCSS", $_], unpack "(a6)*", $data;

Replies are listed 'Best First'.
Re^2: unpack into arrayrefs?
by Anonymous Monk on Feb 22, 2022 at 19:30 UTC

    That is a nice approach.

    I was in a hurry, so went for a quick substr approach:

    push @a, [ unpack "CCSS", substr($data, $_ * 6, 6) ] for 0..(length($data) / 6 - 1);

    Had an off-by-one error at first though. Perl doesn't seem to have a range operator that goes up to n-1, like Ruby does? (0..5 loops from 0 to 5 but 0...5 loops from 0 to 4 in Ruby)

      Perl is smart enough not to need one:

      push @a, [ unpack 'CCSS', substr $data, 0, 6, '' ] while length $data;

      🦛

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-09-29 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (125 votes). Check out past polls.

    Notices?