J
Justin C
I'm reading The Alpaca (Intermediate Perl - yeah, I know, do I *really*
think I'm smart enough to be there yet?) and in Chapter 6 I find the
following example:
my @input_numbers = (1, 2, 4, 8, 16, 32, 64);
my @indices_of_odd_digit_sums = grep {
my $number = $input_numbers[$_];
my $sum;
$sum += $_ for split //, $number;
$sum % 2;
} 0..$#input_numbers;
What has happened to $_ ? In line three I understand that it is one of
the indices of @input_numbers, but in line 5 it has become each of the
digits of @input_numbers. I'm just getting used to map and grep, and
throwing that in there has been a bit confusing. I can see what has
happened, but am having difficulty reconciling that with use of $_ in
grep and map blocks. Is the first $_ clobbered, or is it still there
after the 5th line? ... hmmm, yes it is... not it isn't it's been
clobbered, it's... no, I was right the first time! (it's been a long
week). So, is line 5 a naked block with it's own $_? How do I identify,
in other grep/map blocks, which $_ is $_?!
Justin.
think I'm smart enough to be there yet?) and in Chapter 6 I find the
following example:
my @input_numbers = (1, 2, 4, 8, 16, 32, 64);
my @indices_of_odd_digit_sums = grep {
my $number = $input_numbers[$_];
my $sum;
$sum += $_ for split //, $number;
$sum % 2;
} 0..$#input_numbers;
What has happened to $_ ? In line three I understand that it is one of
the indices of @input_numbers, but in line 5 it has become each of the
digits of @input_numbers. I'm just getting used to map and grep, and
throwing that in there has been a bit confusing. I can see what has
happened, but am having difficulty reconciling that with use of $_ in
grep and map blocks. Is the first $_ clobbered, or is it still there
after the 5th line? ... hmmm, yes it is... not it isn't it's been
clobbered, it's... no, I was right the first time! (it's been a long
week). So, is line 5 a naked block with it's own $_? How do I identify,
in other grep/map blocks, which $_ is $_?!
Justin.