Notes: Perl Lab 02

Outline

  1. Array assignments
  2. Array subscripts
  3. The $#array variable
  4. push and pop
  5. shift and unshift
  6. removing an array

  1. 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:

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 00
    # Description: Show an example of an array assignment and referencing
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three four five six seven eight nine );
    
    # print out the value for the fourth element
    print "Fourth element is $stuff[4]\n";
    
    # assinging values to an array with quotes
    @things = ( "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", 
                "sixteen", "seventeen" );
    
    # print out the value of an element based on a scalar variable
    $element = 5;
    print "Element number $element is $things[$element]\n";
    
    exit;
    # bye!
    
    

    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.
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 01
    # Description: Show an example of an array assignment and referencing
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three );
    
    # assinging values to an array with quotes
    @things = ( "first thing", "second thing", "third thing", "fourth thing" );
    
    # assigning values to an array with other arrays and variables
    @new_array = ( @stuff, $things[2], $things[0] );
    
    # some output showing the merged values in @new_array
    $element = 4;
    print "Element number $element is $new_array[$element]\n";
    
    exit;
    # bye!
    
    

    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.
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 02
    # Description: Show another example of an array assignment.
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three four five 6 7 8 9 10);
    
    # assigning values to an array with a subset of another array
    @another_list = @stuff[2..5];
    
    # show all the elements of the new array
    print "@another_list\n";
    
    # show some of the elements of the new array
    print "@another_list[2,0]\n";
    
    exit;
    # bye!
    
    

    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] 
    

  2. 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:

     
    #!/usr/bin/perl 
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 03
    # Description: An example of array subscripts
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three four five 6 7 8 9 10);
    
    # show the contents of element 4 first
    print "four is $stuff[4]\n";
    
    # now assign element 4 a new value
    $stuff[4] = "yuck";
    
    # show the contents of element 4 again
    print "four is now $stuff[4]\n";
    
    # we can assign a value to a new element of the array
    $stuff[45] = "Yow";
    
    print "number 45 is $stuff[45] but 44 is undefined: $stuff[44]\n";
    
    exit;
    # bye!
    
    

    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] 
    

  3. 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.

     
    #!/usr/bin/perl 
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 04
    # Description: array indexing
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three four five 6 7 8 9 ten );
    
    # this is the awkward way of getting the index number of the last element
    $total = scalar(@stuff) - 1;
    
    # Print the number to the screen
    print "$total is the number of the last element in the array \"stuff\"\n";
    
    # better yet!
    print "$#stuff is the number of the last element of the array and its ";
    print "value is $stuff[$#stuff]\n";
    
    exit;
    # bye!
    
    

    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]
    

  4. 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.

     
    #!/usr/bin/perl 
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 05
    # Description: using push to add to an array
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three four five 6 7 8 9 ten );
    
    # ouptput some information about the array
    print "The last element of stuff is $#stuff with a value of $stuff[$#stuff]\n";
    
    # add a new element to the array
    $new = 600;
    push(@stuff, $new);
    
    # ouptput some information about the updated array 
    print "The last element of stuff is $#stuff with a value of $stuff[$#stuff]\n";
    
    exit;
    # bye!
    
    

    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.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 06
    # Description: using pop to remove elements from an array
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three four five 6 7 8 9 ten );
    
    # ouptput some information about the array
    print "The last element of stuff is $#stuff with a value of $stuff[$#stuff]\n";
    
    # pop off the last element of the array
    $ret_value = pop(@stuff);
    
    # ouptput some information about the updated array 
    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;
    # bye!
    
    

    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]
    

  5. 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.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 07
    # Description: using shift to remove elements from an array
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three four five 6 7 8 9 ten );
    
    # ouptput some information about the array
    print "The first element of \@stuff contains $stuff[0]\n";
    print "The number of the last element of \@stuff is $#stuff\n\n";
    
    # pop off the last element of the array
    $ret_value = shift(@stuff);
    
    # ouptput some information about the updated array 
    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;
    # bye!
    
    

    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.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 08
    # Description: using unshift to insert elements into an array
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three four five 6 7 8 9 ten );
    
    # ouptput some information about the array
    print "The first element of \@stuff contains $stuff[0]\n";
    print "The number of the last element of \@stuff is $#stuff\n\n";
    
    # pop off the last element of the array
    $new = "happy";
    unshift(@stuff, $new);
    
    # ouptput some information about the updated array 
    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;
    # bye!
    
    

    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] 
    

  6. 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.)

     
        
        
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab02 Example 09
    # Description: Deleting arrays
    #==========================================================================
    
    # assinging values to an array with the qw operator
    @stuff = qw( zero one two three four five 6 7 8 9 ten );
    
    # assinging values to an array with quotes
    @things = ( "first thing", "second thing", "third thing", "fourth thing" );
    
    # remove all values from the first array
    $#stuff = -1;
    
    # remove all values from the second array
    @things = ();
    
    print "Is it really gone? $things[0]\n";
    
    # really delete them
    undef @stuff;
    undef @things;
    
    exit;
    # bye!
    

    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