Notes: Perl Lab 02
Outline
- Array assignments
- Array subscripts
- The $#array variable
- push and pop
- shift and unshift
- removing an array
-
Array assignments
In perl, arrays are defined as they are assigned values. There
is no need to define the dimension of the array. Below are some
examples of assigning values to the elements of an array:
|
@stuff = qw( zero one two three four five six seven eight nine );
print "Fourth element is $stuff[4]\n";
@things = ( "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen" );
$element = 5;
print "Element number $element is $things[$element]\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab2_zero.pl
Fourth element is four
Element number 5 is fifteen
[mark@iguana perl]
|
Another example of assinging values to an array. Here the a new
array (new_array) is populated with the values from another
along with some scalar variables. Note that the array
subscripts start with the value 0, not 1.
|
@stuff = qw( zero one two three );
@things = ( "first thing", "second thing", "third thing", "fourth thing" );
@new_array = ( @stuff, $things[2], $things[0] );
$element = 4;
print "Element number $element is $new_array[$element]\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab2_one.pl
Element number 4 is third thing
[mark@iguana perl]
|
More ways of assigning values to an array.
|
@stuff = qw( zero one two three four five 6 7 8 9 10);
@another_list = @stuff[2..5];
print "@another_list\n";
print "@another_list[2,0]\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab2_two.pl
two three four five
four two
[mark@iguana perl]
|
-
Array subscripts
As was shown in the examples above, the values for individual
elements of an array can be accessed as scalar variables by
using subscripts. Subscripts are the number appended in
brackets to the scalar variable with the name of the array.
Values for individual elements of an array can be assigned as
any other scalar variable would. An example follows:
|
@stuff = qw( zero one two three four five 6 7 8 9 10);
print "four is $stuff[4]\n";
$stuff[4] = "yuck";
print "four is now $stuff[4]\n";
$stuff[45] = "Yow";
print "number 45 is $stuff[45] but 44 is undefined: $stuff[44]\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab2_three.pl
four is four
four is now yuck
number 45 is Yow but 44 is undefined:
[mark@iguana perl]
|
-
The $#array variable
Since perl allows arrays to be created without specifying thier
dimensions, we need an easy way of determining the size of the
array. There are two ways of doing this, but one is much more
efficient.
|
@stuff = qw( zero one two three four five 6 7 8 9 ten );
$total = scalar(@stuff) - 1;
print "$total is the number of the last element in the array \"stuff\"\n";
print "$#stuff is the number of the last element of the array and its ";
print "value is $stuff[$#stuff]\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab2_four.pl
10 is the number of the last element in the array "stuff"
10 is the number of the last element of the array and its value is ten
[mark@iguana perl]
|
-
push and pop
Another way of accessing the elements of an array are the
push
and pop
functions.
push
will append a new value into an array creating
a new element at the "end" of the array.
|
@stuff = qw( zero one two three four five 6 7 8 9 ten );
print "The last element of stuff is $#stuff with a value of $stuff[$#stuff]\n";
$new = 600;
push(@stuff, $new);
print "The last element of stuff is $#stuff with a value of $stuff[$#stuff]\n";
exit;
|
When executed, the script above will produce the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab2_five.pl
The last element of stuff is 10 with a value of ten
The last element of stuff is 11 with a value of 600
[mark@iguana perl]
|
pop
does just the reverse of push
,
removing the last element of the array and returning the value
of that element.
|
@stuff = qw( zero one two three four five 6 7 8 9 ten );
print "The last element of stuff is $#stuff with a value of $stuff[$#stuff]\n";
$ret_value = pop(@stuff);
print "pop just pulled off the value $ret_value\n";
print "The last element of stuff is $#stuff with a value of $stuff[$#stuff]\n";
exit;
|
When executed, the script above will produce the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab02_six.pl
The last element of stuff is 10 with a value of ten
pop just pulled off the value ten
The last element of stuff is 9 with a value of 9
[mark@iguana perl]
|
-
shift and unshift
While push
and pop
will alter items
from the "end" of an array, shift
and
unshift
will add or remove items from the
"front" of an array. shift
removes the
first element of the array, returning the value of that
element. The array index numbers are reordered so that the new
first element is 0 and all items in the array are kept in order
but do not keep their original index values.
|
@stuff = qw( zero one two three four five 6 7 8 9 ten );
print "The first element of \@stuff contains $stuff[0]\n";
print "The number of the last element of \@stuff is $#stuff\n\n";
$ret_value = shift(@stuff);
print "shift just pulled off the value $ret_value\n";
print "The first element of stuff now contains $stuff[0]\n";
print "The number of the last element of \@stuff is now $#stuff\n";
exit;
|
When executed, the script above will produce the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab02_seven.pl
The first element of @stuff contains zero
The number of the last element of @stuff is 10
shift just pulled off the value zero
The first element of stuff now contains one
The number of the last element of @stuff is now 9
[mark@iguana perl]
|
unshift
is analogous to push
but it
will insert a new element into an array at the beginning, or
"front", of the array. All items in the array are
renumbered accordingly.
|
@stuff = qw( zero one two three four five 6 7 8 9 ten );
print "The first element of \@stuff contains $stuff[0]\n";
print "The number of the last element of \@stuff is $#stuff\n\n";
$new = "happy";
unshift(@stuff, $new);
print "unshift just inserted the value $new\n";
print "The first element of stuff now contains $stuff[0]\n";
print "The number of the last element of \@stuff is now $#stuff\n";
exit;
|
When executed, the script above will produce the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab02_eight.pl
The first element of @stuff contains zero
The number of the last element of @stuff is 10
unshift just inserted the value happy
The first element of stuff now contains happy
The number of the last element of @stuff is now 11
[mark@iguana perl]
|
-
Removing an array
There are two basic ways to remove an array: One is to empty the
array of all its values. The second is to actually remove the
variable completely with the undef
function. (Of
course, there are other ways of accomplishing this task.)
|
@stuff = qw( zero one two three four five 6 7 8 9 ten );
@things = ( "first thing", "second thing", "third thing", "fourth thing" );
$#stuff = -1;
@things = ();
print "Is it really gone? $things[0]\n";
undef @stuff;
undef @things;
exit;
|
When executed, the script above will produce the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab02_nine.pl
Use of uninitialized value in concatenation (.) or string at ./lab02_nine.pl line 20.
Is it really gone?
[mark@iguana perl]
|
last updated: 18 Mar 2012 13:18