Thanks.# Program that will add or multiply as many numbers that are entered. # Use the program as such: program.pl add 3 2 1 use warnings; use strict; my $rtn; my $len = @ARGV; #Get the number of elements $len = $len - 1; if ($ARGV[0] =~ /add/) { $rtn = add($ARGV[1]..$ARGV[$len]); print "The sum is: $rtn"; } elsif ($ARGV[0] =~ /multiply/) { $rtn = multiply($ARGV[1]..$ARGV[$len]); print "The product is $rtn"; } sub add { my $sum = 0; my @data = @_; for (@data){ $sum += $_; } return $sum; } sub multiply { my $product = 1; my @data = @_; for (@data) { $product *= $_; } return $product; }
In reply to Passing Any Range of Values to a Sub by Dru
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |