Perl for Loops

   



Perl Arrays

Arrays in Perl are actually much more flexible than arrays in other programming languages; in some respects, Perl arrays can be manipulated almost as though they were lists. Listing C.4 demonstrates how to create an empty array and append two scalar values to the array.

Listing C.4 array1.pl

start example
 my($age)  = 30; my($name)  = "Tom"; my($weight) = 200; my(@attributes) = (); push(@attributes, $age); push(@attributes, $weight); print "$name is $attributes[0] old and weighs $attributes[1]\n";
end example

Remarks

You can launch the Perl script array1.pl in Listing C.4 from the command line as follows,

perl -w array1.pl

and the output is as follows:

Tom is 30 old and weighs 200

Perl allows you to 'splice' arrays; that is, you can delete, update, or insert a list of values at any position in an array. The pop function removes an item from the end of an array; the shift function does the same thing to the first element of an array. The push function appends an item to the end of an array.



   



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