pidloop has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: forming a word from an array of its bytes
by jcb (Parson) on Apr 07, 2021 at 22:35 UTC | |
by pidloop (Novice) on Apr 08, 2021 at 04:29 UTC | |
by jcb (Parson) on Apr 08, 2021 at 23:29 UTC |