Perl Arrays

Copy the text below and run it as a Perl script.


# ASSIGNING ELEMENTS TO AN ARRAY

# ("Hansel", "Gretel", "Ted", "Alice") is a list 
# literal to be assigned to a variable of type array
 
@myArray = ("Ted", "Alice", "Hansel", "Gretel");

# if a list contains other lists and is assigned
# to an array, they are conflated into one list

@array1 = ("Hansel", "Gretel");
@array2 = ("Ted", "Alice");

# @myArray is reassigned
@myArray = (@array1, @array2);

print "\n";

print "1. ACCESSING INDIVIDUAL ARRAY ELEMENTS\n\n";

# to access an individual element of an array,
# the notation changes to a '$' symbol, the
# variable name, and the index of the element
# in square brackets
# Note: $myArray and $myArray[0] are two completely
# different variables
 
print "the first array element is:        " . $myArray[0] . "\n";
print "the second array element is:       " . $myArray[1] . "\n";

# negative indices count from the end
print "the last array element is:         " . $myArray[-1] . "\n";

# if an array element does not exist, Perl will 
# ignore it gently
print "the-non existing array element is:" . $myArray[10] . "\n";

print "\n";

print "2. ARRAYS IN SCALAR CONTEXT\n\n";

# operators are aware whether they operate on list
# or scalar variables; 
# that is, they know in what context an operation
# takes place;
# if an array is evaluated in scalar a context; it
# returns the number of elements in the array

$arrayLength = @myArray;
print "Array length: " . $arrayLength . "\n\n"; 

# context can be confusing;
# note how the context changes in the following
# two statements; the first one is the same
# as the previous example; the return value
# is just used implicitly

print "scalar context: " . @myArray . "\n\n";

# when the array is interpolated in a string, however,
# it is evaluated in a list context 
print "list context: @myArray\n";
print "\n";

print "3. PROCESSING EVERY ELEMENT OF AN ARRAY\n\n";

# the scalar context of an array is frequently used
# to iterate over every element of
# an array, for example

for ($i = 1; $i <= @myArray; ++$i)
{
   print "element $i: $myArray[$i-1]\n";
}

print "\n";

print "4. ADDING AND DELETING ELEMENTS AT THE END OF AN ARRAY\n\n";

# elements can be deleted from and added to the
# end of an array by using the pop() and push()
# functions

print "a. pop() and push()\n\n";
# pop() takes an array as an argument and returns
# the deleted list element

$popped = pop(@myArray);

print "popped: $popped\n";
print "array length after pop(): " . @myArray . "\n\n";

for ($i = 1; $i <= @myArray; ++$i)
{
   print "element $i: $myArray[$i-1]\n";
}

print "\n";

# push() takes the target array and a list of
# values as arguments, the latter of which it
# adds to the end of the array; it returns the
# new length of the array

$pushed = push(@myArray, ($popped, "Bozo"));

print "pushed $popped, Bozo\n";
print "array length after push(): " . @myArray . "\n\n";

for ($i = 1; $i <= @myArray; ++$i)
{
   print "element $i: $myArray[$i-1]\n";
}

print "\n";

print "b. shift() and unshift()\n\n";
# shift() and unshift do the same as pop()
# and push(), but work at the front of the array;
# shift() takes an array as an argument and returns
# the deleted list element

$shifted = shift(@myArray);

print "shifted: $shifted\n";
print "array length after shift(): " . @myArray . "\n\n";

for ($i = 1; $i <= @myArray; ++$i)
{
   print "element $i: $myArray[$i-1]\n";
}

print "\n";

# unshift() takes the target array and a list of
# values as arguments, the latter of which it
# adds to the front of the array; it returns the
# new length of the array

$unshifted = unshift(@myArray, $shifted);

print "unshifted: $shifted\n";
print "array length after unshift(): " . $unshifted . "\n\n";

for ($i = 1; $i <= @myArray; ++$i)
{
   print "element $i: $myArray[$i-1]\n";
}

print "\n";