[英]Sum the odd and even indices of an array separately - Perl
I have an array of 11 elements. 我有一个由11个元素组成的数组。 Where I want to sum the odd elements including the first and last elements as one scalar and the evens as another.
我想把奇数元素加在一起,包括第一个和最后一个元素作为一个标量,而另一个元素作为另一个元素。
This is my code I am trying to use map adding 2 to each index to achieve the result but I think I have got it wrong. 这是我的代码我试图使用map为每个索引添加2来实现结果,但我想我错了。
use strict;
use warnings;
use Data::Dumper;
print 'Enter the 11 digiet serial number: ';
chomp( my @barcode = //, <STDIN> );
my @sum1 = map { 2 + $_ } $barcode[1] .. $barcode[11];
my $sum1 = sum Dumper( \@sum1 );
# sum2 = l2 + l4 + l6 + r8 + r10;
printf "{$sum1}";
What is a good way to achieve this? 实现这一目标的好方法是什么?
Sum of even/odd indicies (what you asked for, but not what you want [1] ): 偶数/奇数指数的总和(你要求的,但不是你想要的[1] ):
use List::Util qw( sum ); # Or: sub sum { my $acc; $acc += $_ for @_; $acc }
my $sum_of_even_idxs = sum grep { $_ % 2 == 0 } 0..$#nums;
my $sum_of_odd_idxs = sum grep { $_ % 2 == 1 } 0..$#nums;
Sum of even/odd values (what you also asked for, but not what you want [1] ): 偶数/奇数值的总和(你也要求的,但不是你想要的[1] ):
use List::Util qw( sum ); # Or: sub sum { my $acc; $acc += $_ for @_; $acc }
my $sum_of_even_vals = sum grep { $_ % 2 == 0 } @nums;
my $sum_of_odd_vals = sum grep { $_ % 2 == 1 } @nums;
Sum of values at even/odd indexes (what you appear to want): 偶数/奇数索引的值之和(您看起来想要的):
use List::Util qw( sum ); # Or: sub sum { my $acc; $acc += $_ for @_; $acc }
my $sum_of_vals_at_even_idxs = sum @nums[ grep { $_ % 2 == 0 } 0..$#nums ];
my $sum_of_vals_at_odd_idxs = sum @nums[ grep { $_ % 2 == 1 } 0..$#nums ];
Given that you know how many elements you have, you could use the following: 鉴于您知道您拥有多少元素,您可以使用以下内容:
use List::Util qw( sum ); # Or: sub sum { my $acc; $acc += $_ for @_; $acc }
my $sum_of_vals_at_even_idxs = sum @nums[0,2,4,6,8,10];
my $sum_of_vals_at_odd_idxs = sum @nums[1,3,5,7,9];
Add up values at odd and at even indices 在奇数和偶数索引处添加值
perl -wE'@ary = 1..6;
for (0..$#ary) { $_ & 1 ? $odds += $ary[$_] : $evens += $ary[$_] };
say "odds: $odds, evens: $evens"
'
Note for tests: with even indices (0,2,4) we have (odd!) values (1,3,5), in this ( 1..6
) example 测试注意事项:偶数索引(0,2,4)我们有(奇数!)值(1,3,5),在这个(
1..6
)示例中
You can use the fact that the ?:
operator is assignable 您可以使用
?:
运算符可分配的事实
print 'Enter the 11 digiet serial number: ';
chomp( my @barcode = //, <STDIN> );
my $odd = 0;
my $even = 0;
for (my $index = 0; $index < @barcode; $index++) {
($index % 2 ? $even : $odd) += $barcode[$index];
}
This works by indexing over @barcode
and taking the mod 2 of the index, ie dividing the index by 2 and taking the remainder, and if the remainder is 1 adding that element of @barcode
to $even
otherwise to $odd
. 这可以通过索引
@barcode
并获取索引的mod 2, @barcode
索引除以2并取余数,如果余数为1 @barcode
元素添加到$even
否则为$odd
。
That looks strange until you remember that arrays are 0 based so your first number of the barcode is stored in $barcode[0]
which is an even index. 这看起来很奇怪,直到你记得数组为0,所以你的第一个条形码存储在
$barcode[0]
,这是一个偶数索引。
chomp( my @barcode = //, <STDIN> );
presumably was supposed to have a split
before the //
? 大概应该在
//
之前split
?
@barcode
will have all the characters in the line read, including the newline. @barcode
将读取行中的所有字符,包括换行符。 The chomp will change the final element from a newline to an empty string. chomp会将最后一个元素从换行符更改为空字符串。
Better to chomp first so you just have your digits in the array: 最好先咬一下,这样你就可以在阵列中找到你的数字了:
chomp(my $barcode = <STDIN>);
my @barcode = split //, $barcode;
Another Perl, if the string is of length 11 and contains only digits 另一个Perl,如果字符串长度为11且仅包含数字
$ perl -le ' $_="12345678911"; s/(.)(.)|(.)$/$odd+=$1+$3;$even+=$2/ge; print "odd=$odd; even=$even" '
odd=26; even=21
$
with different input 不同的输入
$ perl -le ' $_="12121212121"; s/(.)(.)|(.)$/$odd+=$1+$3;$even+=$2/ge; print "odd=$odd; even=$even" '
odd=6; even=10
$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.