I am trying to combine grep with a basic subroutine, however it does not work correctly. The output list is identical to the input list...
However, this works...
What am I doing wrong in the first snippet of code?
Thank you.
Code:
my @input = (1,2,4,8,16,32,64,128);
my @output = grep square($_),@input;
sub square{
my $value = shift;
my $result = $value * $value;
return $result;
}
print "All values squared (Subroutine) = @output<p>";
#All values squared (Subroutine) = 1 2 4 8 16 32 64 128
However, this works...
Code:
my @input = (1,2,4,8,16,32,64,128);
my @output = grep square($_),@input;
sub square{
$_ = $_ * $_;
return $_;
}
print "All values squared (Subroutine) = @output<p>";
#All values squared (Subroutine) = 1 4 16 64 256 1024 4096 16384
What am I doing wrong in the first snippet of code?
Thank you.