简体   繁体   中英

Sum the odd and even indices of an array separately - Perl

I have an array of 11 elements. 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.

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] ):

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] ):

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];

  1. I included these in case someone needing these lands on this Q&A.

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

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 .

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.

chomp( my @barcode = //, <STDIN> ); presumably was supposed to have a split before the // ?

@barcode will have all the characters in the line read, including the newline. The chomp will change the final element from a newline to an empty string.

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 -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

$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM