Re: Perl Simple Functions
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);
}
Re: Perl Simple Functions
Thank you! Meanwhile, I managed to do that
Code:
print "Enter a number :";
$num1 = <>;
print "Enter another number :";
$num2 = <>;
@pool = ("$num1","$num2" );
sub addprod
{
$addresult = $_[0] + $_[1];
print "The sum is : $addresult\n";
$prodresult = $_[0] * $_[1];
print "The product is: $prodresult\n";
}
addprod (@pool)
Re: Perl Simple Functions
For info: in perl, no need to declare before the subs. Personally (and I think most people did the same) I put them all one after another at the end. And in principle we declare variables with my (or our if necessary), as that eg
my $addresult = $_[0] + $_[1];
But in your case, no need to create a variable, just do it, with brackets to remain in the digital environment:
print "The sum is : ".($_[0]+$_[1])."\n";
I think it is necessary for your learning to use:
use strict;
at the beginning of your code. It will go wrong when something is not good "clean".
Re: Perl Simple Functions
Quote:
I think it is necessary for your learning to use:
use strict;
at the beginning of your code. It will go wrong when something is not good "clean".
It is especially wrong if we use variables without having declared with my (which declares a variable as local to the block or it is) or as overall (our, jobs scarce, creating an overall package), or as local variable block, which override the block in a variable of the same name already declared (local, mostly reserved for local override of $ _, $, etc)
A basic template for the code can be read
Code:
#!/usr/bin/perl
use strict;
use warnings;
.............................
__END__
This is after __END__ that is not interpreted, you can put the code not valid, while editing, notes, personal ... and documentation in pod format