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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi Monks.

I'm currently improving my Perl by practising Perl idioms and trying to write more 'Perlish' code. I'm currently practising by parsing a binary blob with unpack.

My input is a scalar, and my desired output is an array of arrays.

As I'm from a C background my initial approach is to use the C-like for loop and I end up with code like the following:

#!/usr/bin/env perl use strict; use warnings; use 5.016; use Data::Dumper; my $data = "1ABCD2EFGH3IJKL4MNOP5QRST6UVWX"; my $entry_size = 5; my @out; for (my $off = 0; $off < length $data; $off += $entry_size) { my $item = substr ($data, $off, $entry_size); push @out, [unpack "CA*", $item]; }; print Dumper \@out;

While it works, it seems unnecessarily verbose for Perl and I'm aware I'm trying to write C in Perl.

Therefore, I've been attempting to rewrite the code in a more Perlish manner. I've ended up with the following:

#!/usr/bin/env perl use strict; use warnings; use 5.016; use Data::Dumper; my $data = "1ABCD2EFGH3IJKL4MNOP5QRST6UVWX"; my $entry_size = 5; my @items = unpack "(a$entry_size)*", $data; my @out = map { [unpack "CA*"] } @items; print Dumper \@out;

I'm happy with the map { unpack ... } ... construct as this is clear and concise, but I'm a little less sure about the first unpack to split the $data scalar into the list of items.

Therefore I'm deferring to the wisdom of the Monks, is there a better way to achieve what I am doing? Maybe the approach should be a single unpack "(CA4)*", $data and then rebuilding the child lists?


In reply to Perlish approach to parsing a binary blob by mxb

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 avoiding work at the Monastery: (6)
As of 2024-04-20 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found