http://www.perlmonks.org?node_id=11130974

pidloop has asked for the wisdom of the Perl Monks concerning the following question:

Oh great ones! I have an array containing four integral values, each of which we may assume is less than 256. I wish to combine them into one 32 bit native integer in little-endian format.

The following code does what I want by printing 67305985:

my @array = (1, 2, 3, 4); my $val = 0; for (my $i = 0; $i < 4; $i++) { $val |= $array[$i]<<(8*$i); } printf "%d\n", $val;

which you may convince yourself is correct by running

perl -e 'printf "%d\n", (4<<24)+(3<<16)+(2<<8)+1')

My question is: how can I do this without a loop? I have played around with pack, unpack and join but have not yet found a way.

Many thanks for your time and wisdom.