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