Update TIMTOWTDI, as seen by tye's post below. All a matter of perspective. ; )
In this case, you don't want to use the .. operator (which produces consecutive numbers), you just want to pass a list of values. @ARGV contains this list, except for the first element, which is the operation. So shift off the first element from @ARGV, figure out what to do, pass the rest of @ARGV to the appropriate function, and you should be set.
I'd suggest you figure it out yourself, but here's the code if you really want to see it...
# 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 $cmd = shift @ARGV; if ($cmd =~ /add/) { $rtn = add(@ARGV); print "The sum is: $rtn"; } elsif ($cmd =~ /multiply/) { $rtn = multiply(@ARGV); 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 Re: Passing Any Range of Values to a Sub
by The Mad Hatter
in thread Passing Any Range of Values to a Sub
by Dru
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |