|
|
Listing C.21 displays a Perl script that invokes a custom function in order to calculate the sum of the numbers in an array.
Listing C.21 addArray.pl
my(@numArray) = (); push(@numArray,1); push(@numArray,2); push(@numArray,3); push(@numArray,4); addArray(@numArray); sub addArray(@) { my($index) = 0; my(@rest) = @_; foreach $index (0..$#rest) { $sum += $rest[$index]; } print "The sum of all the numbers is $sum\n"; }
You can launch the Perl script addArray1.pl in Listing C.21 from the command line as follows,
perl -w addArray1.pl
and the output is as follows:
The sum of all the numbers is 10
|
|