\chapter{Manipulation of Data Structures\label{chap:basicds}}

\section{Scalar Variables}

I have qualitatively described how a scalar variable looks like in the previous chapter. Now we are 
going to look at how you can use it in your program. \\

You refer to a variable by appending the identifier of the variable to the symbol \mono{\$}. For 
example, a variable named LuckyNumber is written as \mono{\$LuckyNumber}. \\

\subsection{Assignment}

In Perl, you are not required to declare a variable before it is being used. However, before you
ever use a variable in your program you should give it a default value. To assign a value to the variable we use 
the \term{assignment operator} (=). For example, we can assign the value 18 to the variable \mono{\$LuckyNumber}
as follows: \\

\begin{verbatimtab}[4]
	$LuckyNumber = 18;
\end{verbatimtab}

This statement is interpreted as follows: the value on the right hand side of the assignment operator is 
assigned to the data structure on the left. In this case, 18 is assigned to \mono{\$LuckyNumber}. \\

However, the right hand side of the assignment operator is not confined to a literal only. The right
hand side of an assignment operator is actually treated as an \term{expression}. An expression consists 
of a sequence of operations which evaluate to a value by means of operators. For example, \mono{(6 + 5) * 2} is an expression consisting of two
operations (\mono{*} denotes multiplication). Evaluating an expression means to deduce the result of
the expression, by evaluating each operand, and applying the operators in a certain order (subject
to operator precedence and associativity) to transform the expression into the value. The expression
in this example evaluates to the scalar value 22. You will learn more about operators in the next chapter. 
Therefore, if we have another variable \mono{\$Num} which has the value of 8, executing the statement 
\mono{\$LuckyNumber = \$Num} causes \mono{\$Num} to be evaluated on the right hand side, and thus 
its value, that is 8, is assigned to \mono{\$LuckyNumber}. So this is essentially \mono{\$LuckyNumber} = 8. \\

Cascaded assignment is also allowed, e.g. \mono{\$a = \$b = 8;} First, 8 is assigned to \mono{\$b}, 
and then the value of \mono{\$b} is assigned to \mono{\$a}. The net effect is that the two variables 
both have the value 8. \\

In some other programming languages, to supply a default value before you first use a variable (\term{initialization})
is very important. For C/C++, you need to declare a variable before it is used. This reserves memory space for this 
variable. However, it is not required in C/C++ that a value needs to be assigned during variable declaration. 
A variable in this state is described as \term{undefined}. Getting a value of an undefined variable poses 
a very subtle source of error (some garbage values are returned --- that is, arbitrary value without any meaning) that 
yields surprising results, and is very difficult to debug. In Perl, if you use a variable that is not initialized 
(for example, printing its value), the value \term{undef} (undefined) is returned. This is a special value 
that gives different values in different contexts. Contexts will be introduced in the last section of this 
chapter and, simply put, it is 0 in numeric context (i.e. if a number is expected), an empty string in string 
context (i.e. a string is expected) or FALSE in ``Boolean'' context (i.e. when either TRUE or FALSE is 
expected). However, if you have specified the \mono{-w} switch, the interpreter should have warned you on using 
uninitialized variables. The use of uninitialized variables is not a good practice, and you have to ensure 
that all variables are given a value before being used. \\

As already discussed, you can store scalar data of different types in a variable during the lifetime of 
the script, so you can now assign the string literal ``eight'' to \mono{\$LuckyNumber}. Perl will just 
happily accept it. Of course, you should try to avoid it, as described in the previous chapter. \\

% TODO: should move to "operators" chapter
\specialbox{NOTES}{
You may see the terms \term{lvalue} and \term{rvalue} in some other books or documentation. An 
lvalue refers to any valid entities that can be placed on the left hand side of the assignment operator, 
while an rvalue refers to any valid entities that can be placed on the right hand side of the assignment 
operator. A list, an array or scalar variable can be an lvalue. Literals (scalar in sense) are not 
lvalues. Different programming languages have different lvalues and rvalues. \\

For example, these are lvalues (of course they can be rvalues as well): \\

\mono{
\$var	\\
(\$var, @lst)	\vspace{\baselineskip} \\
}
These are not: \\

\mono{
46	\\
("mystuff", "apple")	\\
}
}

\subsection{Nomenclature}

Before you proceed, I will first talk about how to choose an identifier for a variable (and arrays or hashes alike). 
An identifier should start with a letter (A-Z, a-z) or underscore (\_). Subsequent letters may be alphanumeric 
characters (A-Z, a-z, 0-9) or an underscore (\_). No spaces are allowed in the middle of an identifier. There 
is one more important point. {\em Perl is at all times case-sensitive}. That means it differentiates 
lowercase and uppercase characters. The \mono{print()} function you saw in the Hello World example cannot 
be replaced by Print, PRINT or anything else. Similarly, \mono{\$var} and \mono{\$Var} are two different 
variables. The last point to note is that identifiers cannot be longer than 255 characters (long identifiers are 
time-consuming to type and difficult to interpret --- please avoid them). \\

\specialbox{NOTES}{
If you read on, you will discover that Perl has many builtin predefined variables which do not follow such 
a nomenclature scheme. For example, \mono{\$1} - \mono{\$9} are reserved for backreferencing in pattern matching (see  
Chapter~\ref{chap:regex} ``Regular Expressions''), or other more awkward looking variables like \mono{\$,}, \mono{\$"}, 
\mono{\$\_} ... just to name a few. Because devoting a whole chapter just to introduce them would be too boring, 
I will introduce them as needed throughout the text. Alternatively, the \link{http://www.perldoc.com/perl5.8.0/pod/perlvar.html}{perlvar} manpage describes
these Perl predefined variables in detail. \\
}

Another point to note is that the name of a variable, array or hash is formed by a symbol (\mono{\$} for variable) 
and the identifier. Therefore, \mono{\$Var}, \mono{@Var} and \mono{\%Var} can coexist. Although they have the same identifier, they 
are still unique names because the symbols are different. Also, you may use ``reserved words'' for the identifier, 
e.g. \mono{\$print} because the symbol before the identifier tells Perl that this is a variable. There is thus
no ambiguities. \\

\subsection{Variable Substitution}

It's time to talk about \term{variable substitution}. I told you that single-quoted strings do not 
allow variable substitution, while double-quoted strings can. Variable substitution means that variables
embedded in a double-quoted string will be substituted by their values at the time the statement is evaluated. 
Consider this example: \\

\head{EXAMPLE}{Celsius $\rightarrow$ Fahrenheit Converter}
\lstinputlisting[backgroundcolor=\color{CodeBackground}]{src/datamanip/c2f.pl}

Line 9 looks awkward, but here we perform two operations, to accept user input and remove the trailing newline character. 
You don't have to concern much about this statement yet, as this will be described in Chapter~\ref{chap:files}. \\

Line 11 calculates the Fahrenheit temperature, and when execution of the script reaches line 14, it sees the 
two variables \mono{\$cel} and \mono{\$fah}. Then Perl replaces them with the values the two variables carry 
at that instant, and the resulting string is output to the screen. \\

There is one problem arising from variable substitution. What if we have some words immediately following 
the variable, without even a space? Perl has provided a nice facility to get around this situation. You may put 
a pair of curly brackets around the identifier name to separate it from the surrounding text, e.g. \\

\begin{verbatim}
print "${Num}th Edition";
\end{verbatim}

\subsection{\mono{substr()} --- Extraction of Substrings}

Frequently you have to extract a sequence of characters from a string. In other words, you extract a substring
from it. Perl provides you with the \mono{substr()} function to extract a substring, provided that you already
know in advance where to start extracting it. \\

The syntax of the \mono{substr()} function is as follows: \\

\begin{verbatim}
substr STRING, OFFSET
substr STRING, OFFSET, LENGTH
substr STRING, OFFSET, LENGTH, REPLACEMENT
\end{verbatim}

As you can see, \mono{substr()} can take 2-4 parameters depending on your needs. 
% Because functions are just operators in Perl-think, parameters are just treated as operands in disguise.
\mono{STRING} is the string from which extraction is performed. \mono{OFFSET} is a zero-based offset which
indicates the position from which to start extraction. The first character of any string has an \mono{OFFSET}
0, and 1 for the second character etc. In Perl, \mono{OFFSET} can be negative, which counts from the end.
For example, the last character of the string can be represented by the \mono{OFFSET} -1. \mono{LENGTH} is
the number of characters to extract. If it is not specified, it extracts till the end of the string. The
extracted substring is returned upon evaluation. Here are some examples: \\

\begin{verbatimtab}[4]
$string = "This is test.";
print substr($string, 5);		# is test.
print substr($string, 5, 2);	# is
\end{verbatimtab}

If \mono{REPLACEMENT} is specified, the substring is replaced by the string obtained by evaluating \mono{REPLACEMENT}, and the
substring being replaced is returned. Alternatively, you may put \mono{substr()} on the left hand side
of an assignment operator, and \mono{REPLACEMENT} on the right. Here is an example which replaces a substring
of length 0 with the replacement string ``not a '', that implies inserting it at position 8: \\

\begin{verbatimtab}[4]
substr($string, 8, 0, "not a ");		## First method
substr($string, 8, 0) = "not a ";		## Second method
print $string;					# This is not a test.	
\end{verbatimtab}

\subsection{\mono{length()} --- Length of String}

You can find out the length of a string by using the \mono{length()} function. The only parameter for
the \mono{length()} function is the string itself. It returns the number of characters in the string. \\ 

Scalar variable is a very simple data structure. In the next section we are going to deal with lists, arrays 
and hashes that are a lot more interesting to play with. \\

\section{Lists and Arrays}

Arrays are named entities, lists are not. The relationship between a \term{list} and an array is very much 
similar to that between a literal and a scalar variable. A list is merely an ordered set of elements. An array is just 
like a list but with a name thus can be referenced through an array variable. Studying the behaviour of lists 
allow us to progress naturally into arrays and hashes in subsequent sections. \\

\subsection{Creating an Array}

Each item in the list is called an \term{element}. To create a list, simply delimit (separate) the elements 
with commas (,) and surround the list with a pair of parentheses. For example a list containing the names of 
some colours can be written as \\

\begin{verbatimtab}[4] 
   ("red", "orange", "green", "blue")
\end{verbatimtab}

An array can be created by assigning a list to an array variable. An array variable starts with the symbol \mono{@} 
(compare with the case of \mono{\$} for scalar variables). Therefore, an array can be set up containing the list 
above, e.g. \\

 
\begin{verbatimtab}[4] 
   @colors = ("red", "orange", "green", "blue");
\end{verbatimtab} 

Alternatively, you may use the equivalent method of per-item assignment (to be discussed shortly): \\

\begin{verbatimtab}[4] 
   $colors[0] = "red";    
   $colors[1] = "orange";    
   $colors[2] = "green";    
   $colors[3] = "blue";    
\end{verbatimtab} 

This array contains 4 elements. A null array is simply (). Lists can be nested, that is, a list may contain 
other lists or array variables as an element. However, the embedded lists will be expanded and merged with the 
container list. Any null lists or null arrays are removed. For example, if \\

 
\begin{verbatimtab}[4] 
@unix = ("FreeBSD", "Linux");
@os = ("MacOS", ("Windows NT", "Windows ME"), @unix);
\end{verbatimtab} 

 
\mono{@os} is expanded into \\

\begin{lstlisting}[numbers=none,backgroundcolor=\color{white}]
@os = ("MacOS", "Windows NT", "Windows ME", "FreeBSD", "Linux");
\end{lstlisting}

In the following example, \mono{@result} will become a null array. Note that null lists are ignored. \\

\begin{verbatimtab}[4]
	@nullarray = ();
	@result = ((), @nullarray);
\end{verbatimtab}
 
A useful operator that worths mentioning here is the \term{range operator} (..). If you would like to 
generate an array of consecutive integers this operator may come in handy, as you no longer have to use a loop 
to do it. Example: \\

 
\begin{verbatimtab}[4]
   @hundrednums = (101 .. 200);
\end{verbatimtab}
 
However, the numbers must be in ascending order. If you would like to have an array of consecutive integers 
in descending order, you may construct it in ascending order using the range operator, and then reverse the 
position of the items using the \mono{reverse()} function: \\

\begin{verbatim}
@hundrednums = reverse (101 .. 200);
\end{verbatim}

\subsection{Adding Elements}

We know from the previous section that if we place an array variable or a sublist as an element of a list, 
the array variable or the sublist would be expanded and merged with the parent list, and in this operation the 
identity of the original array variables or sublists are lost. That means, you can no longer tell from the 
resulting list if a particular element originates from a sublist or an array variable. Therefore, it is natural 
to conclude that two arrays can be merged together by this operation: \\

\begin{verbatim}
@CombinedArray = (@Array1, @Array2);
\end{verbatim}
 

The resulting array contains all the elements in \mono{@Array1}, followed by that of \mono{@Array2}. 
To append a scalar element to the end of an array, you can write, for example, \\

\begin{verbatim}
@MyArray = (@MyArray, $NewElement);
\end{verbatim}

We can also append a list of scalar data to the end of an array by using the \mono{push()} function. The 
syntax of the \mono{push()} function is \\

\begin{verbatim}
push ARRAY, LIST;
\end{verbatim}

where ARRAY is the array to which the list data are to be appended. LIST is a list specifying the elements to 
be appended to ARRAY. The mechanism of the \mono{push} function is not much different from the interpolation of 
lists above, and I am more accustomed to the previous one than using the \mono{push} function, because it is
more intuitive to understand. \\

\mono{push} is a function. A function returns some values (not necessary scalar, can be list data as well for
certain functions) after the operation is finished. For this function, the number of elements after element addition 
is returned. Consider the example below: \\
 
\begin{verbatim}
$NumElements = push(@MyArray, @list);
\end{verbatim} 

Note that I have added the parentheses around \mono{@MyArray} and \mono{@list}. The parentheses here 
are not actually necessary, but I added them here to make it obvious the parameters of 
the function. The return value is assigned to \mono{\$NumElements}. In the next chapter, you will learn operator precedence, 
which describes in detail when and where you should add parentheses. For the time being, stick to the way I have been doing 
and it would be fine. \\

On the other hand, this operation inserts the element at the beginning of the array: \\

 
\begin{verbatim}
@MyArray = ($NewElement, @MyArray);
\end{verbatim} 

\mono{unshift} is a function that inserts a list at the beginning of an array, and returns the number of elements 
after the operation. The syntax is \\

\begin{verbatim}
unshift ARRAY, LIST;
\end{verbatim}
 
where ARRAY is the array to which elements are added, and LIST is the list that is inserted at the beginning of 
ARRAY. Consider the following example: \\

\head{EXAMPLE}{Demonstrating unshift()}
\lstinputlisting[backgroundcolor=\color{CodeBackground}]{src/datamanip/unshift.pl}


Again, disregard the line numbers that are for illustration only. Note that the ordering of the items of 
\mono{@alpha1} is preserved in \mono{@alpha2}. What is worth noting is on line 7. We assign a space to an 
odd-looking variable, but what's that? This is an example of Perl predefined variables, and \mono{\$,} is known 
as the \term{output field separator}. Without line 7, you would most likely get an output like ``abcdef'' because 
by default this variable is an empty string. Note that the \mono{print()} function can accept a list of parameters.
The output field separator is the string that is added in between list elements in a \mono{print} operation. 
You may assign any valid string literals to this variable. In this example, I put a space 
in between elements to make them easier to read. \\

\subsection{Getting the number of Elements in an Array}

There are two ways in which you could obtain the number of elements stored in an array. \\

The first method is to employ the concept of context. By evaluating an array in scalar context, we can obtain 
the number of elements in an array. You may not understand what this is for the time being, but we will return to 
this example when we come to contexts later in this chapter. In the following example, the number of elements in 
\mono{@colours} is asssigned to \mono{\$numElements}: \\

\begin{verbatim} 
$numElements = @colours;
\end{verbatim} 

The second method is a little bit clumsy to explain. In Perl, you can find out the subscript of the last item 
of an array by replacing the symbol @ with \$\#. For example, the subscript of the last item in \mono{@Array} is 
given by \mono{\$\#Array}. \\

For historical reasons, Perl provides a facility for users to specify the subscript of the first element of an 
array. This is specified by assigning an integer to the predefined variable \mono{\$[}. This is 0 by default, 
and that's why I said subscripts start from 0. However, some people may be accustomed to using 1 as the starting 
index (especially those who have used some ``antique'' programming languages). This value is not necessarily 0 or 
1. You may set other values as well. However, you cannot assign values to this variable more than once. Although 
Perl provides this facility, you are advised not to use it because of potential confusions that may arise, especially 
if your project consists of a number of files. \\

 

With both the start index and the index of the last element, we can get the number of elements by using the 
formula: \\

 

Number of elements = Last Index - Starting Index + 1 \\
 = \mono{\$\#Array} - \mono{\$[} + 1 \\

 

As the start index is 0 by default (that is, if you don't specify otherwise), you may assume that the number 
of elements of \mono{@Array} is given by \\

\begin{verbatim} 
$#Array + 1
\end{verbatim} 
 

\subsection{Accessing Elements in an Array}

After we have added items to an array or list, we can access any of its elements by using the \term{subscript operator}
([]). For example, if we would like to retrieve the third element (remember that subscripts count from 0) from 
\mono{@colours} and return the value to \mono{\$col3}, we can write \\

\begin{verbatim} 
$col3 = $colours[2];
\end{verbatim} 

Note that the symbol is \$ instead of @ on the right hand side of the assignment operator. In Perl, because the 
value returned is a scalar value, the symbol \$ is used. Though looking 
awkward, you can definitely use the subscript operator on a list, like this: \\

 
\begin{verbatim}
$col3 = ("red", "orange", "green", "blue")[2];
\end{verbatim}

Obviously, using the subscript operator in this way is not really useful. \\

 

What about specifying a negative subscript? Negative subscripts count backwards, from the last element of the 
array. The last element has the subscript -1, and the next-to-last element has the subscript -2. Therefore, the 
first element of the array \mono{@array} has the subscript \mono{-@colours} (due to scalar context). Normally, 
negative subscripts are used as a convenience method to retrieve the last element of an array. At least, 
\mono{@array[-1]} is definitely easier to understand than \mono{@array[\$\#array]}. \\

An \term{array slice} is a subset of elements from all the elements in an array. The subscript operator is not confined 
to one subscript only. You may specify a list of subscripts using the comma operator (,) and the range operator (..). 
You use the range operator to specify subscripts (must be integral) in a given range, and the comma operator to specify 
each subscript individually. Nothing will stop you from using both operators together, as in the following example: \\

\head{EXAMPLE}{Array Slices}
\lstinputlisting[backgroundcolor=\color{CodeBackground}]{src/datamanip/aryslices.pl}

In this example, the array slice \mono{@slice} contains the 5\super{th} element, and 6 elements starting from the
11\super{th} element of \mono{@alpha}. The resulting output is thus ``e k l m n o p''. Note that because the array slice 
contains a list of values, the symbol of \mono{@alpha} is @ on line 4. \\

We can, of course, modify the value of any element in an array. To do this, just assign a scalar value to the 
corresponding array element. Similar is the case for slice assignment, in which a list of scalar data is assigned as a list. 
For example: \\

\begin{verbatim}
$colours[2] = "violet";

# The followings are identical 
@colours[2,4] = ("violet", "blue");
$colours[2] = "violet"; $colours[4] = "blue";
@colours[4,2] = ("blue", "violet");
\end{verbatim} 

If you specify a subscript that is larger than \mono{\$\#array}, the 
size of the array shall grow in order to accommodate the newly added element. In this example, because \mono{@nums}
contains originally 3 elements, assigning a value to the 5\super{th} element leaves a ``gap'' at the 4\super{th} element, 
not assigned any values. The value of this element is \mono{undef}. \\

\head{EXAMPLE}{Array Expansion}
\lstinputlisting[backgroundcolor=\color{CodeBackground}]{src/datamanip/aryexpansion.pl}

To show that there is an empty gap at the 4\super{th} element, the output list separator is set to \bs n. So you 
would see a line like ``Use of uninitialized value at eg0304.pl line 6'' between 5 and 7 in the output if warnings 
are enabled. That's because retrieving the value of an uninitialized value produces this Perl warning. \\

\subsection{Removing Elements}

We can use the \mono{pop} function to remove the last element of an array. It also returns the value of the 
item being removed. Syntax: \\

\begin{verbatimtab}[4]
	pop ARRAY;
\end{verbatimtab}

In this example, the last item of \mono{@MyArray} is removed and its value is assigned to \mono{\$retval}: \\

\begin{verbatimtab}[4]
	$retval = pop(@MyArray);
\end{verbatimtab}

On the other hand, the \mono{shift} function removes the first element of the array, so that the size of the 
array is reduced by 1 and the element immediately after the item being removed becomes the first element of the 
array. It also returns the value of the item being removed. Syntax: \\

\begin{verbatimtab}[4]
	shift ARRAY;
\end{verbatimtab}

If ARRAY is empty, \mono{undef} is returned. \\


\subsection{\mono{splice()}: the Versatile Function}


There is a generalized function for adding and removing elements from an array. The \mono{splice} function 
is so general that it can do what \mono{push}, \mono{pop}, \mono{shift} and \mono{unshift} does. The 
syntax is: \\

\mono{splice ARRAY, OFFSET \textsl{[, LENGTH [, LIST]]};} \vspace{\baselineskip} \\

In this tutorial, the parts in slanted font denote optional parameters. These parameters are optional in 
the sense that in some situations they are optional; but not in other situations. I use [] to label that a 
parameter is optional. Note that [LIST] is placed within another optional parameter LENGTH. This means that 
if you have to specify the parameter LIST, you must also specify LENGTH; but not vice versa. If you specify 
the parameter LENGTH, you may or may not supply the parameter LIST. \\


In general, this function removes LENGTH elements starting from the element of subscript OFFSET of ARRAY, 
and inserts LIST at OFFSET if any. Simply put, the list \mono{@ARRAY[OFFSET .. OFFSET + LENGTH - 1]} is 
replaced by LIST. The syntax shows that this function takes three forms, and I am going to describe them one 
by one. \\

\begin{verbatim}
splice ARRAY, OFFSET, LENGTH, LIST
\end{verbatim}

This performs exactly the action above. In the example below, an array containing the 26 lowercase alphabets 
was built, and 5 elements starting from the 5\super{th} element (i.e. the letter ``e'') is converted to uppercase. 
This is done on line 2. First \mono{@alpha[4 .. 8]} contains the 5 letters that are to be converted to uppercase 
(remember that subscripts start at 0). The \mono{map} function calls the \mono{uc} function (uppercase) for 
every element in this list, thus converting \mono{("e", "f", "g", "h", "i")} to \mono{("E", "F", "G", "H", "I")}. 
The \mono{splice} function, therefore, replaces the lowercase list with the uppercase one. \mono{uc} and 
\mono{map} would be covered later. \\

\head{EXAMPLE}{splice}
\lstinputlisting[backgroundcolor=\color{CodeBackground}]{src/datamanip/splice.pl}

What you will see on the screen should be all lowercase alphabets except E, F, G, H and I. \\

\begin{verbatim}
splice ARRAY, OFFSET, LENGTH
\end{verbatim}

If you don't specify the replacement list, the action is merely removing LENGTH elements from ARRAY starting 
from subscript OFFSET. \\

\begin{verbatim}
splice ARRAY, OFFSET
\end{verbatim}

If you don't specify the LENGTH, Perl assumes that all elements starting from OFFSET are removed. OFFSET 
is just a subscript and can be negative as well. A negative value specifies the OFFSET is counted from the 
end of the array as mentioned earlier, e.g. -1 means the last item, -2 means the second last item etc. 
Therefore, \mono{pop @MyArray} can be equivalently accomplished by \mono{splice @MyArray, -1}. \\


The following table summarizes how you can use \mono{splice} in place of other functions discussed earlier (still remember 
that in Perl you always have a number of ways to do the same task?). You may find the equivalent method for \mono{push()}
strange, but after you have learnt the whole theory behind contexts you would understand it. We would use this 
illustration again when we come to contexts. \\

\begin{table}[htbp]
\begin{center}
\begin{tabular}{ll}
\hline
\textbf{Function}	&	\textbf{Equivalent Method}	\\
\hline 
	\mono{push(@Array, \$x, \$y)}	&	\mono{splice(@Array, @Array, 0, \$x, \$y)} \br \\
	\mono{pop(@Array)}	&	\mono{splice(@Array, -1)} \br \\
	\mono{shift(@Array)}	&	\mono{splice(@Array, 0, 1)} \br \\
	\mono{unshift(@Array, @x)}	&	\mono{splice(@Array, 0, 0, @x)} \br \\
	\mono{\$Array[\$x] = \$y}	&	\mono{splice(@Array, \$x, 1, \$y)} \br \\
\hline
\end{tabular}
\end{center}
\caption{Relationship of some array functions with splice()}\label{tab:splice}
\end{table}

\subsection{Miscellaneous List-Related Functions}

There are a number of useful functions that allow you to manipulate list data in Perl. \\

\begin{verbatim}
join STRING, LIST
\end{verbatim}

The \mono{join()} function concatenates a list of scalars into a single string. It 
takes a \mono{STRING} as its first argument which is the separator to be put in between
the list elements \mono{LIST}. For example, \\

\begin{verbatimtab}[4]
	join '+', 'apple', ('orange', 'banana')
\end{verbatimtab}

evaluates to \\

\begin{verbatimtab}[4]
	apple+orange+banana
\end{verbatimtab}

\begin{verbatim}
reverse LIST
\end{verbatim}

In a list context, the \mono{reverse()} function returns a list whose elements are 
identical to that of \mono{LIST} except the order is reversed. For example: \\

\begin{verbatimtab}[4]
	print join " ", reverse 'a'..'e';	# e d c b a
\end{verbatimtab}

The \mono{map()} function takes on one of the following two forms: \\

\begin{verbatim}
map BLOCK LIST
map EXPR, LIST
\end{verbatim}

The \mono{map()} function iterates over every item in the \mono{LIST}, sets \mono{\$\_} to the
item concerned and executes \mono{BLOCK} or \mono{EXPR} on each iteration. The return value is
a list consisting of the result of evaluation of all iterations. \mono{BLOCK} is a code block
enclosing a sequence of statements to be executed. \mono{EXPR} can be any expression. Consider
this example: \\

\begin{verbatimtab}[4]
@names = ('ALICE', 'tOm', 'JaSON', 'peter');
print join(', ', map { ucfirst(lc($_)); } @names), "\n";
\end{verbatimtab}

The output is \\

\begin{verbatim}
Alice, Tom, Jason, Peter
\end{verbatim}

This example prints out each of the names in \mono{@names} so that they all start with capital
letters while the other characters are in lowercase. This is accomplished by first converting all 
characters to lowercase by the \mono{lc()} function, and the first letter is capitalized using
the \mono{ucfirst()} function. This process is performed for each name in \mono{@names}. The
same expression may be rewritten as \\

\begin{verbatimtab}[4]
@names = ('ALICE', 'tOm', 'JaSON', 'peter');
print join(', ', map (ucfirst(lc($_)), @names)), "\n";
\end{verbatimtab}

The first form of \mono{map()} is generally preferred to the second one because it is more flexible. \\

The \mono{sort()} function can be used to sort a list. By default, the \mono{sort()} function sorts
lexicographically. The items are ordered by comparing the items stringwise (using the \mono{cmp} operator,
the specifics of which will be introduced in the next chapter). This comparison is case-sensitive, because
it is based on the ASCII values of each character. The sorted list is returned by the \mono{sort()} 
function, while the original list remains intact. \\

\begin{verbatimtab}[4]
sort ('bear', 'Post', 'ant');	# ('Post', 'ant', 'bear')
\end{verbatimtab}

Note that while comparing stringwise, capital letters are considered ``smaller'' than lowercase letters. \\

To make the sort routine generic and allow sorting in any arbitary order, you may override the default
sort criteria with your own rules. The method is to insert a code block before the list, similar to the
case for \mono{map()}. The value resulted from evaluation of the block determines how the items are sorted. \\

The principle of constructing the contents of the block is too advanced at this stage. The rest of the 
details can be found in the next chapter.
In the following I list several most commonly used sort criteria that you are likely to find useful: \\

\begin{verbatimtab}[4]
sort { $a <=> $b } @list;	# ascending numerical order
sort { $b <=> $a } @list;	# descending numerical order
sort { $a cmp $b } @list;	# ascending lexicographical order (default)
sort { $b cmp $a } @list;	# descending lexicographical order

# case-insensitive ascending lexicographical order
sort { lc($a) cmp lc($b) } @list;		# or use uc()
\end{verbatimtab}

\subsection{Check for Existence of Elements in an Array (Avoid!)}

Many new programmers, and programmers who have programmed in other languages tend to use arrays so extensively 
that are sometimes inappropriate from performance considerations. One classical problem is to deduce if a 
particular piece of scalar data matches any of the elements stored in an array (in alternative terminology, 
check for a \term{hit} or a \term{miss}). In some languages like BASIC where a hash is not a builtin type, there 
is still little excuse to solve this problem by searching an array, although you can still implement a hash 
yourself (For those who are interested in implementing a hash themselves, please read Appendix~\ref{chap:advhash}). \\

In general, you can check if a certain element exists in an array by \term{linear search}. That is, you search 
from the first element up to the end of the array. In the following code snippet, a random list containing 100 
entries is generated randomly and you can enter a number to be searched. The program then searches for the number. 
When the search ends, it returns whether it is found, and how many elements the program has searched. \\

\head{EXAMPLE}{Linear Search of an Array}
\lstinputlisting[backgroundcolor=\color{CodeBackground}]{src/datamanip/linearsearch.pl}

The code itself is not very important here. In fact, many new constructs used in this program have not been 
discussed yet. The intent is for you to run the program instead of reading the source code (but you may do it). 
Try to look at the list of values printed, and try to enter a number that is not in the array, a number that appears 
early in the list, and a number that appears around the end of the list. A fraction is printed on the screen. 
The one on the left is the number of comparisons performed, while the one on the right is the number of elements 
in the array (which should be 100 in this example). You may also want to increase \mono{\$NUM} to increase the number of 
integers generated. Of course, in this case you will need to increase \mono{\$MAXINT} accordingly to minimize 
the chance of duplication of values. \\

Note that it is possible to have duplicate values in the array owing to the random nature of the random number
generator. In this program, the first occurrence (i.e. with the lowest subscript) will be returned. \\

An alternative scheme is to use \term{binary search}, a rather classical topic in elementary computer science 
texts on algorithms. By using binary search, the whole set of values needn't be searched in the worst case. In 
the worst case only $log_{2}n$ comparisons are required. However, there is an important requirement --- the list 
needs to be already sorted. If not, sorting needs to be performed first. Here is the above example rewritten 
that employs the binary search algorithm: \\

\head{EXAMPLE}{Binary Search of an Array}
\lstinputlisting[backgroundcolor=\color{CodeBackground}]{src/datamanip/binarysearch.pl}

This implementation is even more complicated than that of the linear search. What is worth noting is the 
very low number of comparisons required in any cases (For 100 integers the fraction printed in any case should 
be at most 7/100 \footnote{The maximum number of comparisons can be calculated by $ceiling(log_{2}(n))$ where 
$ceiling(y)$ is the minimum integer that is greater than or equal to y. In this case $ceiling(log_{2}(100)) = 7$.}). 
That means among the 100 integers in the list, at most 7 elements searched is sufficient to deduce whether any 
specified number exists in the array. In Chapter 5, as an exercise, you would be asked to convert this program 
into the recursive form. \\

Binary search works as follows. The list is sorted in either ascending or descending order (assume we have the 
list sorted in ascending order here). In this algorithm we always maintain two important variables --- \mono{\$start} 
and \mono{\$end}. Initially they are set to 0 and 99 (the subscript of last element) respectively. Then the 
element in the middle (subscript 44) is examined and compared with the number to be searched. If the number 
examined is larger than the input number, that means the input number, if exists, would have a subscript less 
than 44, so we can shrink the range by reducing \mono{\$end} to 43. Otherwise it would have a subscript greater 
than 44, so we increase \mono{\$start} to 45. Such a bisection process is repeated with this new set of \mono{\$start} 
and \mono{\$end}, until the number is found or \mono{\$end} is less than \mono{\$start}. Note that on each 
iteration the range examined is halved, and that's why the maximum number of comparisons is $log_{2}(n)$. \\

An example of binary sort is shown in Figure~\ref{fig:binarysort}. 18 is to be searched and is found on the third 
iteration. The red boxes denote the element in the middle of the range being searched, and those in grey are 
those that are bypassed. \\

\image{!hbt}{!}{!}{images/binarysort.eps}{An illustration of binary sort}{fig:binarysort}

Although it seems binary search performs a lot better compared with linear search, there is an important catch here --- 
most arrays we encounter are not sorted, so the cost of sorting cannot be simply ignored in practice. \\

As you can possibly see in both cases when \mono{\$NUM} is large, neither approach is performing in an efficient 
manner, especially when misses occur because in the linear search case, every element in the array has to be searched 
in the worst case (when a miss occurs); while in the binary search case, the array needs to be sorted first, and the 
average sorting time of an array grows with the array size \footnote{In algorithmic analysis, the best sort algorithms
can attain the time complexity of $O(n log n)$. That is, the sorting time $t$ is in the form $t = Kn log(n)$ where $K$ is
a constant and $n$ is the size of the array. The function grows even faster than $n$, so performance degrades would be
evident at relatively large values of $n$.}
, so combining sorting and searching may be even slower than the linear search algorithm. Towards the end of the next section 
we shall redo this code with a hash, and you ought to find the code can be made cleaner and a hit or a miss can be determined 
faster in general, especially with large array sizes. \\

\section{Hashes}

Hash is a special kind of data structure. There are several characteristics associated with it. It practically takes 
very short time to deduce whether any specified data exists. Also, the time it takes does not largely depend on the number 
of items stored. This is important because hashes are usually used for applications that handle a large amount of data. \\

An array is simply a contiguous block of memory and is nothing more than that. In order to support the characteristic 
stated above, hashes require a slightly more complicated internal structure. This is outlined in Appendix~\ref{chap:advhash} for your reference. 
It explains the general principles that further Perl knowledge is not necessary in order to understand it. However, in this 
section we are dealing with how we use hashes in Perl, and will not discuss the peculiarities of them here. \\

As a quick review, each item in a hash has a key and a value. The key, which is a string, uniquely identifies an item 
in the hash. The value is any form of scalar data. Hash variables start with the symbol \%. \\

\subsection{Assignment}

We may assign a list to a hash variable. In this case, the list will be broken up two-by-two, the first one as the key 
and the second as the value: \\

\begin{verbatimtab}[4] 
    %Age = ('Tom', 26, 'Peter', 51, 'Jones', 23);
\end{verbatimtab} 

Because a hash contains multiple key-value pairs, this alternative syntax may seem more intuitive to look at: \\
 
\begin{verbatimtab}[4]
    %Age = ('Tom' => 26, 'Peter' => 51, 'Jones' => 23);
\end{verbatimtab} 

The symbol \mono{=>} is defined as an almost equivalent symbol to the comma, so in general anywhere a comma is needed 
you can replace it with this symbol. However, the use of \mono{=>} is particularly intuitive for data that occur in pairs. 
In addition, Perl allows even less keystrokes by omitting the quotation marks: \\
 
\begin{verbatimtab}[4]
    %Age = (Tom => 26, Peter => 51, Jones => 23);
\end{verbatimtab} 

This is because Perl always interprets the word before the symbol \mono{=>} as a double-quoted string. However, if you 
omit the quotes, the key cannot have embedded whitespace (space, tabs and so on). To specify a key with embedded
whitespace, the quotes must be specified. Also, because Perl uses the semicolon, not a newline, to mark the 
termination of a statement, you can make the hash assignment better to look at by writing it in multiline form: \\

\begin{verbatimtab}[4]
%Age = (
	Tom => 26,
	Peter => 51,
	Jones => 23,
);
\end{verbatimtab}

\specialbox{NOTES}{
One note about why we can omit the quotes here. According to the perldata manpage, 
a word that has no other interpretation in the grammar will be treated as if it were a 
quoted string. For example, if an unquoted word is not a reserved word, filehandles, 
labels etc. Perl will automatically treat it as a bareword. However, avoid barewords 
consisting entirely of lowercase letters, because all Perl reserved words are in 
lowercase. As Perl is case-sensitive, this eliminates the possibility of potential 
name clashes in future versions of Perl. \\
}

Note that on the final line there seems to be a superfluous comma there. Yes, it 
is. The final comma before the closing parentheses can be omitted. But it is customary 
to have it there in the above multiline form because it is likely that you may append 
more key-value pairs later on. Then you will have to add the comma back, and it will 
result in an error if you have omitted to do so. Bear in mind that the four formats above 
are identical, and you may choose the form which looks best to you. An empty hash is an 
empty list assigned to a hash variable, similar to the case of arrays. \\

Because in the assignment operation Perl expects a list as the rvalue, apart from 
lists you may as well assign an array, or even a subroutine returning a list to the 
hash variable. Note that the hash variable provides a list context here. \\

If we assign a hash variable to an array, or anywhere a list is expected, the 
key-value pairs stored in the hash will be returned in list form. However, because 
of the way the key-value pairs are stored in a hash, they may be (actually most 
likely) returned in an order different from when they were put into the hash. \\

\subsection{Accessing elements in the Hash}

Accessing an element in a hash is similar to that from an array, except we replace 
square brackets with curly ones and instead of an index, the key is used. Here's an example: \\

\begin{verbatimtab}[4]
    print $Age{Tom};
\end{verbatimtab} 

As you will learn in Chapter~\ref{chap:eval}, a word appearing inside the curly braces  
not enclosed by quotes is evaluated as a string. Therefore, 
Tom is equivalent to `Tom' in this case. To associate a scalar value to a key is as simple as: \\

\begin{verbatimtab}[4]
    $Hash{'Key'} = $value;
\end{verbatimtab}

If Key already exists, it is assigned the supplied value; otherwise, a new key-value pair 
is added to the hash. \\

\subsection{Removing Elements from a Hash}

Perl provides the \mono{delete} function for removing a specified key-value pair from a 
hash. Here's an example: \\

\begin{verbatimtab}[4]
    delete $Age{Tom};
\end{verbatimtab} 

This function returns the deleted scalar value associated with the specified key(s). So 
if you \mono{print} the return value in the above statement, the value(s) deleted would be 
displayed on screen. \\

To delete all key-value pairs in a hash, you can of course use a loop to do it, but this 
is slow and it would be more efficient to use either method below: \\

\begin{verbatimtab}[4]
    %Age = ();
\end{verbatimtab} 

or 

\begin{verbatimtab}[4]
   undef %Age;
\end{verbatimtab} 

Here is a special example for \mono{delete} that is worth mentioning: \\

\begin{lstlisting}[backgroundcolor=\color{CodeBackground}]
%Age = (
	Tom => 26,
	Peter => 51,
	Jones => 23,
);
@temp = delete @Age{'Tom', 'Peter'};
$, = " ";
print "Deleted values:", @temp, "\n";
print "Remaining keys:", keys @Age;
\end{lstlisting}

On line 6, we delete multiple key-value pairs from \mono{\%Age}. Note that 
the function returns a list of values associated with the deleted keys, so the 
symbol used should be \mono{@}. \\

Line 9 uses the \mono{keys()} function. It returns a list of keys in 
the specified hash. There is a corresponding \mono{values()} function that 
returns a list of values contained in the hash. Recall the \mono{sort()} function
mentioned earlier. Apart from arrays,  
you may use the \mono{sort()} function together with the \mono{keys} and \mono{values} 
function to specify how to order the hash items. For example, \\

\begin{verbatimtab}[4]
%array = (
	'3' => 'apple',
	'11' => 'orange',
	'5' => 'banana',
);
@key = sort { $a <=> $b } keys %array;		# ('3', '5', '11')
@value = sort { $a cmp $b } values %array;		# ('apple', 'banana', 'orange')
\end{verbatimtab}

\subsection{Searching for an Element in a Hash}

You may test if a particular key exists in a hash by using the \mono{exists} 
function. However, even if the key exists in the hash, the value associated may be 
undefined. Use the \mono{defined} function to test if the value is defined. \\

Recall earlier we had a program that generated 100 integers in random and 
you look for the existence of a particular number in the list. We now present 
the version using a hash. The number of comparisons taken is not shown, as 
you cannot get this information with a builtin hash. Of course if you build a 
hash yourself you would be able to see the efficiency of hashes. At least, you
will find the implementation is a lot easier compared with the two previous
approaches. \\

\head{EXAMPLE}{Searching for an Element in a hash}
\lstinputlisting[backgroundcolor=\color{CodeBackground}]{src/datamanip/hashsearch.pl}

Notice how clean it is to determine whether the number exists in the hash in 
this case (line 24). In this example, the numbers are stored as keys in the hash. 
We use the \mono{exists()} function to check if the key exists in the hash. This function 
returns TRUE if the specified key exists. Note that you can as well use \mono{defined()}
in place of \mono{exists()} in this example. This function returns true only if the 
key exists in the hash, and the value is not undefined (i.e. \mono{undef}). \\

Table~\ref{tab:definedexists} summarizes the difference between \mono{defined()} and \mono{exists()}: \\

\begin{table}[htbp]
\begin{center}
\begin{tabular}{lccc}
\hline
\textbf{Function}	&	\textbf{Key Exists}	& 	\textbf{Value Undefined}	&	\textbf{Return Value}	\\
\hline 
	\mono{exists()}	&	Yes &  No 	& TRUE \br \\
						&	Yes &  Yes 	& TRUE \br \\
						&	No 	&  N/A 	& FALSE \br \\
						\br 
	\mono{defined()}	&	Yes	&  No	& TRUE \br \\
						&	Yes	&  Yes	& FALSE \br \\
						&	No	&  N/A	& FALSE \br \\
\hline
\end{tabular}
\end{center}
\caption{Differences between exists() and defined()}\label{tab:definedexists}
\end{table}

In the example, because 0 (or anything except \mono{undef}) is assigned as value to each key put 
into the hash, therefore, \mono{exists()} and \mono{defined()} yields the same results. \\

Owing to the nature of a hash, duplicate keys are not allowed. However, as I have previously noted 
it is possible that by using such a random generation scheme duplicate elements may be generated, and in 
this case exactly only 1 instance is stored, so the number of elements in the hash in this example can 
be less than 100. \\

\section{Contexts}

The idea of contexts in Perl may appear a bit odd at first glance, but you would soon discover that 
contexts are actually quite intuitive to understand, because there are many real life examples resembling 
contexts in Perl. \\

First, look at these two phrases. Note that the same word ``press'' appears in both sentences, but 
their meanings in the phrases are entirely different. \\

\textbf{Press} the button \\
Freedom of \textbf{press} \\

The word ``press'' plays different roles in the two sentences, one acting as a verb and the other 
as a noun. Therefore, to deduce the meaning of the word in the phrase, we have to look at the words 
surrounding it, in other words, the \term{context}. \\

I'm sure you would find many other examples that exemplify how contexts play their roles in our 
daily lives, so I am not going to spend too much time mentioning things you may have already known. 
Recall that earlier in this chapter I obtained the number of elements of an array by using an 
assignment like this: \\

\begin{verbatimtab}[4]
   $numElements = @colours;
\end{verbatimtab} 

You may find this assignment rather peculiar. It seems that I have been assigning list data to 
scalar data, and they actually don't match! How can a scalar data store an array? \\

Yes. That's impossible (unless by using references - that we will see at a later part of this 
tutorial). Also recall that I mentioned earlier that I was ``evaluating an array in scalar context''. 
Now I am telling you what this phrase means. \\

Because you are now assigning something to a scalar variable, the data type expected on the 
right hand side of the assignment operator is naturally a scalar value. In this way, we have created 
a scalar context around the array \mono{@colours}. Because a scalar value is requested instead of 
an array, Perl defines that by evaluating an array in scalar context, the number of elements stored 
in the array is returned and the value of which is assigned to \mono{\$numElements}. \\
 
It is important to bear in mind that the rules as to how a data type is evaluated in another context 
depend on the definition, and there is no general rules of inference. Later on, when you are to write 
subroutines, you will be taught to use the \mono{wantarray()} function to determine the contexts so 
that you can specify what to return from your subroutine in different contexts. \\

Remember I have mentioned Boolean context earlier in this chapter? Actually there is not a ``Boolean''
context, because Perl does not have an intrinsic Boolean data type. Perl achieves this by using scalar 
context instead. Perl defines that the numeric zero (0), the empty string (``'') and the undefined value 
(\mono{undef}) are interpreted as FALSE, and all other scalar values are interpreted as TRUE. Therefore, 
anywhere a Boolean test is expected you can place any scalar value there instead, and Perl shall evaluate 
it according to this rule. \\

Sometimes you may want to evaluate a list in scalar context, but it is in list context instead. 
A useful function about contexts is \mono{scalar}, which provides the necessary scalar context, as 
in the following example: \\

\begin{verbatimtab}[4] 
   print scalar(@array);
\end{verbatimtab}

Without the \mono{scalar} function, the array is evaluated in list context and so the contents 
of the array will be \mono{print}ed. However, if we would like to \mono{print} the number of elements 
in the array instead, the \mono{scalar} function provides the necessary scalar context for this purpose. \\

I do not aim at telling you everything about how different contexts give rise to different behaviours 
when we deal with operators at the moment. Instead, it is important to realize that operators can exhibit 
different behaviours depending on the context they are in. You are going to have a boring day probably reading 
the next chapter, as you are going to know in more details how each operator is affected by contexts. \\

\section{Miscellaneous Issues with Lists}

Before we start a new chapter, let us pay attention to several issues concerning lists which have not 
yet been mentioned above. \\

You have seen in the previous section that, because of evaluation of an array variable in scalar context, 
the number of elements in the array is returned: \\

\begin{verbatimtab}[4]
   $a = @array;
\end{verbatimtab}

but things get entirely different when you write it this way: \\

 
\begin{verbatimtab}[4]
   $a = (35, 48, 56);
\end{verbatimtab}
 
This is a special case, because \textit{in scalar context} the list on the right of the assignment operator is 
actually a list of values delimited by the \term{comma operator} (,). This operator is borrowed from C/C++ and 
has nothing to do with lists or arrays. The behaviour of this operator is, evaluate each of the expressions (in 
this case, numbers) delimited by commas, and return the value resulting from the last expression. Therefore, in 
this example 56 is returned and assigned to \mono{\$a}. This applies to void context as well. While \\

\begin{verbatimtab}[4]
   @array = (35, 48, 56);
\end{verbatimtab}

the commas there act as list argument separators. As a rule, always bear in mind that a list is only a list 
in list context. In other contexts the rules of comma operators apply. \\

Thanks to the flexible syntax of Perl, we can assign multiple values concurrently, like this (this is called 
list assignment): \\

\begin{verbatimtab}[4] 
   ($a, $b) = (11, 22);
\end{verbatimtab}

Both sides are lists of the same size, so this is just a mapping: 11 is assigned to \mono{\$a} and 22 to 
\mono{\$b}. What about if the list on the right has more elements than the one on the left? Then the extra 
elements are simply ignored. What about if the list on the left has more elements? Then some variables in the 
left hand list shall receive the value \mono{undef}, as you may expect. To swap the values of two variables, 
an operation that requires an additional temporary variable in most other programming languages, you can perform
it in Perl simply by: \\

\begin{verbatimtab}[4]
   ($x, $y) = ($y, $x);
\end{verbatimtab}

Consider this example and try to guess what happens: \\

\begin{verbatimtab}[4] 
   ($a, @b) = (11, 22, 33, 44);
\end{verbatimtab} 

11 is assigned to \mono{\$a}. The remaining three elements are assigned to \mono{@b}. But what if you do this? \\

\begin{verbatimtab}[4] 
   (@a, $b) = (11, 22, 33, 44);
\end{verbatimtab} 

In this case, \mono{@a} gobbles up all the values in the list, leaving the value \mono{undef} to 
\mono{\$b}. We describe this behaviour as ``greedy''. \\

In this chapter, you have learnt how to use the three fundamental data structures in Perl, namely variables, 
arrays and hashes. I have also tried to give you some ideas on the concept of contexts, which is of fundamental
importance in Perl. In the next chapter, I will introduce to you the operators available in Perl. \\

\section*{Summary}

\begin{summary}
\item Scalar variables are prepended with the symbol \mono{\$}. For array and hash variables, the symbols are \mono{@} and \mono{\%} respectively.
\item Scalar variables can be assigned a value by using the assignment operator \mono{=}. A variable holds the value \mono{undef} by default.
\item Variable names are case-sensitive. They should start with an alphabet or an underscore character (\_). Subsequent characters may be decimal digits as well. Variables whose names not conforming to this nomenclature are predefined by Perl and serve special purposes.
\item Variable substitution means variables embedded in a double-quoted string will be substituted by their respective values at the instant the string is evaluated.
\item The \mono{substr()} function extracts a sequence of characters from a given string.
\item The \mono{length()} function returns the number of characters in a given string.
\item A list is an ordered set of scalar values. An array is a list associated with a name.
\item An array may be created by assigning a list to an array variable.
\item Nested lists are merged to form a single list when the list is evaluated, with null lists removed.
\item The \mono{reverse()} function returns a list whose items are identical to the input list except the items are arranged in reverse order.
\item The \mono{push()} function may be used to append a list of items to the end of an array.
\item The \mono{unshift()} function prepends a list of items to the beginning of an array.
\item The subscript operator \mono{[]} can be used to access a subset of elements in a list or an array.
\item The \mono{pop()} and \mono{shift()} functions may be used to remove an item from an array. \mono{pop()} removes the last item while \mono{shift()} removes the first item.
\item \mono{splice()} is a general purpose function to add or remove array elements.
\item \mono{join()} concatenates a list of scalars into a string, inserting a string in between. 
\item \mono{map()} executes a given code block for each list element, and the results evaluated are combined to form an array.
\item Searching an array is not efficient. A hash should be used instead.
\item A hash may be initialized by assigning it a list, whose elements are treated as a list of key-value pairs.
\item To refer to a hash element, use curly braces with the key in between.
\item The \mono{delete()} function may be used to remove a key-value pair from a hash.
\item You may use the \mono{exists()} function to test if a given key exists in the hash. The \mono{defined()} returns if a value is not \mono{undef}.
\item Behaviours of Perl operators and functions are influenced by context. The \mono{scalar()} function forces a scalar context in an otherwise list context.
\item The result of evaluating an array in scalar context is the number of items in the array. Evaluating a list in scalar or void context causes all list items to be evaluated, and the value of the last item is returned.
\end{summary}
