The Perl join Function

   



Passing Arrays to Perl Functions

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

start example
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";  }
end example

Remarks

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



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net