|
|
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
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";
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.
|
|