For example, the number 178 should convert to the letter "M".
178 is 10110010.
Reversing all of the bits should give 01001101, which is 77 or "M" as a character. I taught about using the Reverse function but I don't know how can I use it on an @array.
use strict;
use warnings 'all';
open(my $fh1, '<', 'sym.dat') or die $!;
open(my $fh2, '<', 'sym2.txt') or die $!;
open my $fh_out, '>', 'symout.txt' or die $!;
until ( eof $fh1 or eof $fh2 ) {
my @l1 = map hex, split '', <$fh1>;
my @l2 = map hex, split '', <$fh2>;
my $n = @l2 > @l1 ? @l2 : @l1;
my @sum = map {
no warnings 'uninitialized';
$l1[$_] + $l2[$_];
} 0 .. $n-1;
@sum = map { split //, sprintf '%08X', $_ } @sum;
print { $fh_out } "reverse @sum\n";
}
I am calculating here the sum of hex values but the question is the same I want to reverse the byte values.
You call reverse()
on an array, by just passing the array to the function. However, like all Perl functions, you can't put a function call inside a string. So instead of
print { $fh_out } "reverse @sum\n";
You want:
print { $fh_out } reverse(@sum), "\n";
The parentheses around @sum
are required here to prevent the newline from being included in the arguments to reverse
.