ArraysArrays are used when you want to deal with many things at one time. And when you want to keep those things in a certain order.Perl has arrays just like C, Java and C++ but, of course, it does it in a different and very convenient way. The magic letter preceding an array identifier is the at sign '@'.
Two mnemonics:
Initializing an arrayOn the right hand side we put a parenthesized comma separated list of scalars.@nums = (1, 3, 2, 5, 4); Other examples:
The last example shows that you don't have to
have the same kind of value in each element of an array.
Just scalars.
38-43 86-87 69
ReferencingTo reference an individual element of an array we put an integer in square brackets - this number is called the 'index' (This is not the same as the function 'index'). The first element of the array is at index 0 not 1.Notice that the preceding character is '$' not '@'. Individual elements are scalars.print $nums[3]; # prints 5 print $names[1]; # prints Jon The last one above extends the array @nums (as initialized above). Elements that are as yet unassigned have the value of 'undef'. Arrays are quite dynamic - they grow and shrink as needed. This does certainly not happen in C, C++ or Java.$nums[1] = 8; # replaces the 3 with 8. $nums[$i+1] = $n*sqrt(56); $nums[10] = 11; 38-43 91-92 69-70
Last index and SizeThe index of the last element of @nums is $#nums. This syntax is punctuationally intense but does work. If you assign an array to a scalar the scalar will get the current size of the array.40,49 89-90,98 74@counts = (1, 34, 2, 56, 234); print $#counts; # prints 4 $n = @counts; print $n; # prints 5
Printing and InterpolatingArrays and arrary elements can be printed and interpolated just like scalars:Note that spaces are inserted between elements when interpolating but that when you give an array to print its elements are all concatenated to each other - just as if you had given each element as a separate parameter to print.@nums = (1, 2, 3); print "@nums"; # 1 2 3 print "the 3rd number is $nums[2]\n"; print @nums; # 123 45-46 89 62
Clearing an array to zero size@vals = (); Assigning arrays@nums = (1, 2, 4, 5); @counts = @nums; Pop, Push, Shift, UnshiftThe functions 'pop' and 'push' operate on the end of the array.In the same way, 'shift' and 'unshift' work on the beginning of the array.@nums = (11, 3, 2, 4); $n = pop @nums; # removes 4 $m = pop @nums; # removes 2 push @nums, 12; # @nums is now (11, 3, 12) 44-45 100-102 71@nums = (11, 3, 2, 4); $n = shift @nums; # removes 11 $m = shift @nums; # removes 3 unshift @nums, 45; # @nums is now (45, 2, 4)
Reading all Lines into an arrayHow can you read all the lines of a file into an array? There are many ways. A favorite 'motto' of the Perl community is this:
First way:
Second way:
Third way:
Cryptic? Perhaps. But very nice.@names = <IN>; 51-52 185 never
Command line argumentsWhen you invoke a perl program (e.g. hello) on the command line you can put extra arguments after the program name:
Command line arguments are automatically put into
an array named @ARGV. You can use it just like
any other array. It is no different except that
it is initialized for you. Study on this code:
72 133-134 102-103
Looping over each element of an arrayAgain, there are many ways.First way:
Note that @names appears in scalar context and
hence the comparison is done against the size of the array.
Second way:
This way will leave the array empty - probably not what
you wanted.
Third way:
Nice, huh!
If you modify $n within the loop it will
change the elements of the array so be careful!
You can actually use 'for' as a synonym for 'foreach'. The 'for' statement has two different syntaxes. The one we saw earlier - with the 3 parts (initialization, test, increment) within the parentheses separated by semicolons ';' like so:
and the other with an array inside the parentheses as above.
46-47,142-144 129-130 88
The more you learn about the way that Perl deals with arrays the less you will use indexes. Sometimes indexes are necessary and sometimes they make for clearer code. Exercises
|