Title: Perl Lists and Arrays
1Perl Lists and Arrays
- CSC 492 Topics in Perl
- Joe Reynoldson
- January 19, 2005
2 Getting Plural in Perl
- List literals
- An unnamed collection of scalars separated by
commas and enclosed by parenthesis - Lists can contain any combination of scalar
values - Arrays
- Named variables which store lists
- The type identifier for arrays is _at_
- The array namespace is separate from the scalar
namespace, so list and _at_list are 2 different
variables
(5, 7, 9, 11) (string, 0, undef,
var1) () (undef)
3Accessing array elements
- Arrays are indexed by integers starting at zero
- The type identifier of an single array element is
- All array subscripts are converted to integers
- Subscripts can also be expressions
- The subscript of the last element in an array is
stored in the special variable array_name
_at_name_list () create an empty array first
0 name_list0 'joe' assign the first
element name_list1.5 'molly' assign the
second element print name_listfirst,"\n"
prints joe print name_listfirst 1,"\n"
prints molly print name_listname_list,"\n"
also prints molly
4Array sizes
- Perl will automatically make an array larger as
necessary - There are several methods to change the size of
an array
_at_small_list (1, 2, 3) 3 items _at_big_list
(_at_small_list, _at_small_list) 6 items big_list
499 add 494 items _at_small_list () the empty
list _at_big_list undef 1 item
5List Shortcuts
- The range operator ( x..y ) creates a list
starting from its left-hand operand and going
up to its right-hand operand - Create a list of strings without all of the
quotes using the qw operator - Assign to a list of scalar variables in one
statement
first_num 1 last_num 100 _at_numbers
(first_num..last_num) numbers 99
_at_sammy qw/ turkey mayo cheese lettuce tomato
/ _at_paths qw( /usr/bin /usr/local/bin
/usr/X/bin )
(fruit,vegetable,meat)qw( pineapple asparagus
beef ) (array0, array1) (array1,
array0) swap!
6Array operators
- push adds onto the end of an array
- pop removes elements from the end of an array
- unshift adds elements to the beginning of an
array - shift removes elements from the beginning of an
array
count 0 while(count lt 10) push(_at_numbers,
count)
while(last_number pop(_at_numbers)) print
last_number
unshift(_at_numbers, (1 .. 10))
while(_at_numbers) print shift(_at_numbers)
Check out Llama book pp 46-47 for more examples
7Array interpolation
- arrays within double quotes will be interpolated
- printing email addresses becomes tricky (llama
page 48) - printing just the array results in no spaces
_at_array (1 .. 10) print "_at_array\n" 1 2 3 4 5
6 7 8 9 10
print _at_array,"\n" 12345678910
8My favorite loop is foreach
- use foreach to iterate over a list of elements
- modifying the control variable modifies the
element of the array
foreach line (_at_file) print "line" scope
'206.176.3' foreach number (1..255) print
"nslookup scope.number\n"
foreach line (_at_file) chomp(line)
line . "\r\n"
9The Cryptic _
- the 'default variable' is _
- many Perl functions use the default variable
- How do you know if a function supports _?
foreach (20..30) if(_ 2) print
10Reversing a list
- the reverse operator returns the reverse of a
list or array - doesn't alter the original
- reverse alone isn't very useful, and -w lets us
know
_at_numbers (1..5) _at_numbers reverse _at_numbers
good reverse _at_numbers still reversed print
reverse _at_numbers prints in increasing order
Useless use of reverse in void context at
scriptname line line_number
11Sorting it all out
- The sort operator returns the sorted version of
a list or array - It sorts 'ASCIIbetically'
- This operator also doesnt modify the original
list or array - The warning flag/pragma can also generate a
useful message about useless use of sort in a
void context
_at_sorted sort qw( quarterly_report.txt
graph.jpg johnson.eml ) print
"_at_sorted\n" graph.jpg johnson.eml
quarterly_report.txt
12Context
- Page 51 of the Llama book says this is the most
important concept of the entire book! - Scalar context - Perl expects a scalar value as a
result of an expression - List context - Perl expects a list value as a
result of an expression
a b simple example of a scalar
assignment lista b also scalar
assignment number exponent binary
assignment is scalar context
_at_a _at_b simple list assignment (a, b, c)
qw/ ay bee see / also list context foreach
name (_at_name_list) list context without
assignment
Page 53 of the Llama book has some better
examples
13Lists in scalar context
- Using a list in scalar context is a kind of
depends - Assigning an array to a scalar stores its size
- The scalar function forces a list or array into
scalar context
_at_a (1..4) create a 4 element list s _at_a
store the size of the list in s print "Size s
" print the size of the list print "Index
a\n" print the last index Size 4 Index 3
print scalar _at_a, "\n" print the size of _at_a
14Scalars in list context
- This is much more straightforward when a scalar
is used in list context it is treated as a list
containing one item
_at_name_list 'joe' a list of one foreach name
('joe') print name list of one again
15ltSTDINgt in list context
- The diamond operator will return everything until
End Of File (EOF) in list context - Each item (delimited by a newline) represents 1
element in the resulting list - EOF from the keyboard in UNIX is CTL-D
- removing the new lines couldn't be easier
_at_input_list ltSTDINgt list context
chomp(_at_input_list ltSTDINgt) chomp em all!
16Interesting comments from the Llama
Page 46 a beginning Perl programmer (wanting
to see how Perls speed compares to Cs) will
take, say, a sorting algorithm optimized for C
(with many array index operations), rewrite it
straightforward in Perl (again, with many index
operations) and wonder why its so slow. The
answer is that using a Stradivarius violin to
pound nails should not be considered a sound
construction technique.
Page 55 Perl will generally waste memory to
save time. This is a good tradeoff if youre
short of memory, you can buy more if youre
short on time, youre hosed.