Perl 中的校验和
Checksum in Perl
我是一名java开发人员,我需要了解一些perl代码并需要在java中进行转换。这是片段。
my @var = map hex,split //,A8F000000F4241;
my $sum=0;
# calculate checksum of odd bytes and twisted even bytes
$sum-=$_ for map((0,2,4,6,8,10,12,14,1,3,5,7,9,11,13,15)[$_], @var[0,2,4,6,8,10,12]), @var[1,3,5,7,9,11,13];
print $sum ;
我真的很难理解第 4 行中它在做什么。任何帮助将不胜感激。
脚本应重写为:
#!/usr/bin/perl
use Modern::Perl;
use Data::Dump qw(dump);
# Split the string into a list of all character
my @elem = split //, 'A8F000000F4241';
dump @elem;
# convert each element to a hexadecimal number
my @var = map hex, @elem;
dump @var;
# list all number between 0 and 15, even then odd
my @numbers = (0,2,4,6,8,10,12,14,1,3,5,7,9,11,13,15);
# >Pick number in @numbers at position defined in @var (even position in @var)
my @l = map($numbers[$_], @var[0,2,4,6,8,10,12]);
dump@l;
# initialization
my $sum=0;
# loop on the two arrays
for (@l, @var[1,3,5,7,9,11,13]) {
$sum -= $_ ;
}
print $sum ;
你看清楚了吗?
我是一名java开发人员,我需要了解一些perl代码并需要在java中进行转换。这是片段。
my @var = map hex,split //,A8F000000F4241;
my $sum=0;
# calculate checksum of odd bytes and twisted even bytes
$sum-=$_ for map((0,2,4,6,8,10,12,14,1,3,5,7,9,11,13,15)[$_], @var[0,2,4,6,8,10,12]), @var[1,3,5,7,9,11,13];
print $sum ;
我真的很难理解第 4 行中它在做什么。任何帮助将不胜感激。
脚本应重写为:
#!/usr/bin/perl
use Modern::Perl;
use Data::Dump qw(dump);
# Split the string into a list of all character
my @elem = split //, 'A8F000000F4241';
dump @elem;
# convert each element to a hexadecimal number
my @var = map hex, @elem;
dump @var;
# list all number between 0 and 15, even then odd
my @numbers = (0,2,4,6,8,10,12,14,1,3,5,7,9,11,13,15);
# >Pick number in @numbers at position defined in @var (even position in @var)
my @l = map($numbers[$_], @var[0,2,4,6,8,10,12]);
dump@l;
# initialization
my $sum=0;
# loop on the two arrays
for (@l, @var[1,3,5,7,9,11,13]) {
$sum -= $_ ;
}
print $sum ;
你看清楚了吗?