Perl 04
Perl 04
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 the
$c[ 4 ] 1543
array is preceded by $, rather
than @, because the individual $c[ 5 ] -89
array elements are scalar $c[ 6 ] 0
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 value false statements) variable
of control variable has
been reached
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
14 "with: @array2\n",
The first argument to function splice is the
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 anThe array. is the offset into the
third argument is the length array— of the
Starting Thefrom element
fourth
i.e., the index ofslice 5
argument
the first in @array,
is a list toall
element to modify in the six
replace
16 to modify.
17 # remove 3 elements, beginning witharray.
elements
element 15 theof
of
@array2
specified replace six elements of
@array slice of the array. If this
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
@array has no elements remaining
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
The Roman numerals for three, five and eight are: III V VIII