chapter04.ppt

4
Array and Hash
Variables
CGI/Perl
Programming
By Diane Zak
1
4
Objectives
• In this chapter, you will:
•
•
•
•
•
Create an array
Access the variables in an array
Create a hash
Access the variables in a hash
Learn how to code the repetition
structure using the foreach and for
statements
2
4
Introduction
• Perl has 3 data types for
variables:
– scalar
• Can store 1 value
– array
• Can store multiple values
– hash
• Can store multiple values
3
4
Juniper Printers Script
• Planning and Coding:
• Output:
4
4
Juniper Printers Script
5
4
Array Variables
• Array Variable or Array
– Group of related scalar variables,
each having the same name
– Index is used to distinguish the
scalar variables within the array
• The index is assigned as the area is
created
• Index starts with 0 for the first
variable
• Index is also known as a subscript
6
4
Array Variables
• Array declaration syntax:
– my arrayname=(list);
• Name of array must begin with at sign (@)
• After the @, the name must start with a
letter, and then a combination of letters,
numbers, or underscores
• The list consists of values separated by
commas
7
4
Array Variables
• Array declaration examples:
– my @sales = (25000, 35000,
10000);
– my @cities = (“Boston”, “Chicago”,
“Detriot”, “San Diego”);
8
4
Array Variables
• Accessing the array
– Replace the @ in the array name with a
$, array name, index enclosed in square
brackets ([ ])
– Example:
– my @sales = (25000, 35000, 10000);
• $sales[0]=25000
• $sales[1]=35000
• $sales[2]=10000
– A scalar variable within an array can be
used the same as any other scalar
variable
9
Using an Array in the
Juniper Printers Script
4
• The customer will be sending
the model number from the form
– 0, 1, or 2
• This number corresponds to the
index number of the @models
array
10
Using an Array in the
Juniper Printers Script
4
• This code now
includes
declaring the
@models array,
as well as
accessing and
printing the
corresponding
model name
11
4
Hash Variables
• Hash variable, or hash:
– Collection of related scalar variables
– Like an array variable
– Instead of using an index number, a
hash uses a key name
– Like an array’s index numbers, the
keys are assigned to the scalar
variables when the hash is created
– Another name for a hash is an
associative array
12
4
Hash Variables
• Hash declaration syntax:
– my hashname = (key1, value1,
key2, value2,...keyn, valuen);
• Name of hash must start with percent
sign (%)
• After %, the name must start with a
letter, and then a combination of
letters, numbers, or underscores
– Can declare the hash in one line,
or multiple lines
13
4
Hash Variables
• Hash declaration example:
– my %sales =
(“Jan”, 25000,
“Feb”, 35000,
“Mar”, 10000);
– “Jan”, “Feb”, and “Mar” are keys
– Their corresponding values are
25000, 35000, 10000
14
4
Hash Variables
• Hash declaration example:
– my %cities =
(“617”, “Boston”,
“312”, “Chicago”,
“313”, “Detroit”,
“619”, “San Diego”);
– Keys: “617”, “312”, “313”, “619”
– Values: “Boston”, “Chicago”, “Detroit”,
“San Diego”
15
4
Hash Variables
• Accessing the hash:
– Replace the % in the hash name
with a $, array name, key enclosed
in braces ({ })
– Example:
– my %sales =
(“Jan”, 25000,
“Feb”, 35000,
“Mar”, 10000);
• $sales{Jan} = 25000
• $sales{Feb} = 35000
• $sales{Mar} = 10000
16
4
Hash Variables
• Accessing the hash:
– If a key has a space:
• Use single or double quotation marks
within the braces
• Example:
– $state {‘New Mexico’}
– $state {“New Mexico”}
– A scalar variable within a hash can
be used the same as any other
scalar variable
17
Using a Hash in the
Juniper Printers Script
4
• The customer will be sending the
letter corresponding to the
Operating System
• The system letter will be stored in
the $sysletter variable
• To access the full operating system
name, the $sysletter key can be
used with the %systems hash
– $systems {$sysletter}
18
Using a Hash in the
Juniper Printers Script
4
• This code now
includes declaring
the %systems
hash, and printing
out the full
operating system
name by accessing
the %systems hash
19
4
Modifying the Juniper
Printers Form and Script
• HTML checkboxes:
– Use the same key, in this form, System
– Syntax:
• <INPUT TYPE=checkbox NAME=name
VALUE=value>
• The CHECKED keyword can be used to have a
checkbox checked by default
– If multiple checkboxes are selected, the
keys and values are sent to the script
• Example:
• If all 3 operating systems are selected:
System=W&System=M&System=U will be
passed
20
4
Modifying the Juniper
Printers Form and Script
• One change
made involves
storing the
System key in
@sysletter array
instead of
$sysletter, due
to the
possibility of
multiple values
for that key 21
4
Modifying the Juniper
Printers Form and Script
22
The foreach and for
Statements
4
• 3 basic structures (control or logic
structures) make up scripts:
– Sequence
• Script statements are processed in the order
they appear in the script
– Selection
• Make a decision or comparison, and then select
one of 2 paths based on the result
– Repetition (loop)
• Repeat a block of instructions for a specified
number of times or until a condition is met
• Examples: foreach, for, while, until
23
The foreach and for
Statements
4
• foreach:
– The foreach statement repeats one or
more instructions per element in a
group, like an array
– When each member of the array has
been processed, the loop stops
– Syntax:
foreach element (group) {
One or more statements processed per
element in group
}
24
The foreach and for
Statements
• foreach
– Example:
4
Result:
my ($num, @numbers);
5000
@numbers = (5000, 200, 100, 3);
foreach $num (@numbers) {
print “$num<BR>\n”;
}
200
100
3
25
The foreach and for
Statements
4
• for:
– The for statement is used to repeat one
or more statements as long as the loop
condition is true
– 3 arguments are used:
• initialization argument
– counter variable
• loop condition
– Boolean expression that evaluates to true or
false
– Loop stops when loop condition evaluates to
false
• update
– Updates the counter variable in the
initialization argument
26
The foreach and for
Statements
4
• for:
– Syntax:
for (initialization; loop condition; update) {
one or more statements to be processed
as long as the loop condition is true
}
– Example:
Result:
my $num;
for ($num = 1; $num < 4; $num = $num + 1) { 1
print “$num<BR>\n”;
}
2
3
27
Updating the Juniper
Printers script
4
• foreach will be
used to
process each
member of the
@sysletter
array
28
Summary
4
• keys are the names of the form elements.
– values of the keys will be passed to the server
– Textbox: value is the text entered
– Radio button or checkbox: value assign to the
VALUE property of the selected radio button or
checkbox
• Array variable, or array, is a group of related
scalar variables.
– Each scalar variable in array has same name, but
unique index number
– First scalar variable in array has index number of 0
• Array declaration: my arrayname = (list);
29
Summary
4
• Array name must start with at sign (@),
followed by letter, then optionally one or more
letters, numbers, or underscores
• When referring to scalar variable in an array,
replace @ with $, then the name, along with
the index enclosed in square brackets ([ ])
• Hash variable, or hash, is a group of related
scalar variables. Each scalar variable has
same name, but unique key
• Hash declaration: my hashname = (key1,
value1, key2, value2, ... keyn, valuen);
30
Summary
4
• Name of hash must start with percent sign
(%), followed by letter, then optionally one or
more letters, numbers, or underscores
• When referring to scalar variable in a hash,
replace % with a $, and follow the name with
the scalar variable’s key in braces ({ }).
• A repetition structure, or loop, is used to tell
the computer to repeat a block of
instructions:
– Certain number of times
– Until a condition is met
31
4
Summary
• foreach statement can be used to repeat
one or more instructions for each
element in a group
• for statement can be used to repeat one
or more statements for as long as loop
condition is true
32