in reply to do not understand grep example
The grep line my @odd_digit_sum = grep digit_sum_is_odd($_), @input numbers;
passes each number into the subroutine using $_. Within the subroutine that value is assigned to $input:
my $input = shift;
The digits of each number are then split into the array @digits
my @digits = split //, $input;
At this point, if $input is 32; then @digits contains: ( '3', '2' ).
Those digits are then summed
$sum += $_ for @digits;
giving 5, and then tested to see if the result is odd:
return $sum % 2;
Which in the case of 32 is true, so true is returned to grep and grep allows that input (32) through to the results array.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: do not understand grep example
by live4tech (Sexton) on Jun 16, 2012 at 06:28 UTC |