Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
my $packed = join '', unpack '(a3x)*', pack 'l*', @sint24s;

Rather than the above,
    my $packed = pack '(l<X)*', @array;
is more concise, probably faster (but this latter not checked).

A shame there seems to be no way to get rid of the final  map on the unpacking side.

>perl -wMstrict -le "use Test::More 'no_plan'; use List::Util qw(shuffle); ;; sub rand_s24 { return map { int(rand 2**24) - 2**23 } 1 .. $_[0]; } ;; use constant MIN_S24 => -(2**23); use constant MAX_S24 => -MIN_S24() - 1; use constant VALUES => (0, 1, -1, MIN_S24, MAX_S24); use constant N_VALUES => scalar(@{[ VALUES ]}); use constant MAX_LEN => 25; ;; for my $pass (1 .. 200) { for my $len (1 .. MAX_LEN) { my @to_pack = (rand_s24($len - N_VALUES), VALUES)[-$len .. -1]; @to_pack = shuffle @to_pack; $len == @to_pack or die qq{bad len: array to pack}; ;; my $packed = pack '(l<X)*', @to_pack; length($packed) == 3 * $len or die qq{bad len: packed string}; ;; my @unpacked = map { unpack('l<', qq{\x00$_})/256 } unpack '(a3)*', $packed; $len == @unpacked or die qq{bad len: unpacked array}; ;; is_deeply \@unpacked, \@to_pack, sprintf('pass %d: len %d', $pass, $len); } } " ok 1 - pass 1: len 1 ok 2 - pass 1: len 2 ... (4996 lines elided) ... ok 4999 - pass 200: len 24 ok 5000 - pass 200: len 25 1..5000

In reply to Re^2: 3-byte representation (Simplified!) by AnomalousMonk
in thread 3-byte representation by gerleu

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 meditating upon the Monastery: (5)
As of 2024-04-23 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found