Module 5 Resource
Module 5 Resource
Outline
4.1 Introduction
4.2 Additional Data Types
4.3 Lists and List Context
4.4 Arrays
4.5 Creating and Manipulating an Array
4.6 Repetition with the for Structure
4.7 Additional Examples of Creating Arrays
4.8 Array Manipulation
4.9 Array Functions
4.10 List Functions
4.11 Searching a Sorted Array
4.11.1 Linear Search
4.11.2 Binary Search
4.12 Introduction to Hashes
4.13 Creating and Manipulating a Hash
4.14 Hash-related Functions
4.15 Internet and World Wide Web Resources
$c[ 0 ] -45
$c[ 1 ] 6
$c[ 2 ] 0
$c[ 3 ] 72
Note: Each element of
$c[ 4 ] 1543
the array is preceded
by $, rather than @, $c[ 5 ] -89
because the individual $c[ 6 ] 0
array elements are
scalar values. $c[ 7 ] 62
$c[ 8 ] -3
$c[ 9 ] 1
$c[ 10 ] 6453
$c[ 11 ] 78
counter
$i = 0= 1
true
$i < 10 print "$i\n"; ++$i
Body of loop Increment
(this may be many the control
Determine if final false statements) variable
value of control
variable has been
reached
a b c d e f g h i j k l m n o p q r s t u v w x y z
@array[ 9 ] is nine.
@array[ -1 ] is nine.
@array[ -4 ] is six.
$chiquita = banana
$dole = pineapple
zero 1 two 3 four 5 six 7 eight 9
$first = 1
@array2 = 2 3 4 5 6 7 8
$second =
1
1 2 Function shift removes and returns the
1 2 3
first element of the array.
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
$firstTotal = 15
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
$secondTotal = 15
2001 Prentice Hall, Inc. All rights reserved.
1 #!/usr/bin/perl Outline
2 # Figure 4.11: splice.pl
3 # Demonstrating function splice.
4
5 # create two arrays and display their initial contents
6 @array = ( 0 .. 20 );
7 @array2 = ( A .. F );
8 print "\@array: @array\n"; Create two arrays and display their
9 print "\@array2: @array2\n\n"; contents.
10
11 # replace part of @array with the elements from @array2
12 @replaced = splice( @array, 5, scalar( @array2 ), @array2 );
13 print "replaced: @replaced\n",
Removes three elements from @array starting with
The first argument to function splice is the
14 "with: @array2\n", Function
element
array splice
15. The
to modify. returnedremoves
list isor replaces
stored in @removed.
15 "resulting in: @array\n\n"; The second argument
slices of anThearray. is the offset into the
third argument is the length array— of the
StartingThefrom
i.e., the index ofslice element
fourth 5
argument
the first in is a
@array,list toall
element to modify in the six
replace
16 to modify.
17 # remove 3 elements, beginning witharray. elements
element theof
15 specified
of @array slice
@array2 replace
of the six elements
array. If this of
18 @removed = splice( @array, 15, 3 );
@array. The listis of
argument replaced
omitted, theelements returned
slice is simply
Removes all elements from subscript 8 to the end of
19 print "removed: @removed\n",
from splice
removedisfrom storedtheinarray.
@replaced.
@array.
20 "leaving: @array\n\n";
21
22 # remove all elements from element 8 to the end of @array
23 @chopped = splice( @array, 8 );
24 print "removed: @chopped\n",
25 "leaving: @array\n\n";
26
2001 Prentice Hall, Inc. All rights reserved.
27 # delete all remaining elements Outline
28 splice( @array );
Deletes all remaining elements in
29
@array.
30 unless ( @array ) {
31 print "\@array has no elements remaining\n";
32 }
@array: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
@array2: A B C D E F
replaced: 5 6 7 8 9 10
with: A B C D E F
resulting in: 0 1 2 3 4 A B C D E F 11 12 13 14 15 16 17 18 19 20
removed: 15 16 17
leaving: 0 1 2 3 4 A B C D E F 11 12 13 14 18 19 20
removed: D E F 11 12 13 14 18 19 20
leaving: 0 1 2 3 4 A B C
Original: 0 1 2 3 4 5 6 7 8 9
Reversed: 9 8 7 6 5 4 3 2 1 0
Unsorted: 100 23 9 75 5 10 2 50 7 96 1 40
Lexically: 1 10 100 2 23 40 5 50 7 75 9 96
Numerically: 1 2 5 7 9 10 23 40 50 75 96 100
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
------------------------------------------------------------
0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28
16 18 20 22* 24 26 28
24 26* 28
24*
25 not found
Found 8 at subscript 4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
------------------------------------------------------------
0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28
0 2 4 6* 8 10 12
Found 6 at subscript 3
%hash
height150width300colorblue
The Roman numerals for three, five and eight are: III V VIII