When you do that:
Code:
print "Enter a number :";
$num1 = <>;
$num1 will contain what you typed, ie a string terminated by a newline. So @pool contains two string terminated by \n and when you do:
Code:
print "Sum of @pool is $result";
when @pool is valued, its content is printed, the two strings with \n, or line breaks obtained. Why add your works anyway? because when you do it:
the + strength assessment of $item (which is a string terminated by \n) in a digital environment, and therefore it is evaluating as $item number is added to $sum.
If you do:
Code:
print "Enter a number :";
$num1 = 0 + <>;
It will force the evaluation of digital input and store it as a numeric value in $num1 (and you'll see the difference in print from @pool: you'll just number without newline)
Another point:
It would be better to write:
@pool = ($num1, $num2);
pool will contain a copy of the values of $num1 and $num2 evaluated when the assignment.
Finally, I do not know if what you're trying to look like this, but it should give you some ideas:
Code:
sub add_mul
{
$sum = 0;
$mul = 1;
foreach $item (@_)
{
$sum += $item;
$mul *= $item;
}
return ($sum, $mul);
}
@result = add_mul(@pool);
print "Sum of @pool is $result[0] and Mul is $result[1]";
After the procedure can be written in a slightly more condensed, but it's just a matter of style:
Code:
sub add_mul {
my ($sum, $mul) = (0, 1);
foreach (@_) {
($sum, $mul) = ($sum + $_, $mul * $_);
}
return ($sum, $mul);
}
Bookmarks