Array and Hash Variables

32
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak

description

Array and Hash Variables. CGI/Perl Programming By Diane Zak. 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. - PowerPoint PPT Presentation

Transcript of Array and Hash Variables

Page 1: Array and Hash Variables

4

1

Array and Hash Variables

CGI/Perl Programming

By Diane Zak

Page 2: Array and Hash Variables

4

2

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

Page 3: Array and Hash Variables

4

3

Introduction

• Perl has 3 data types for variables:– scalar

•Can store 1 value

– array•Can store multiple values

– hash•Can store multiple values

Page 4: Array and Hash Variables

4

4

Juniper Printers Script

• Planning and Coding:

• Output:

Page 5: Array and Hash Variables

4

5

Juniper Printers Script

Page 6: Array and Hash Variables

4

6

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

Page 7: Array and Hash Variables

4

7

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

Page 8: Array and Hash Variables

4

8

Array Variables

• Array declaration examples:– my @sales = (25000, 35000, 10000);– my @cities = (“Boston”, “Chicago”,

“Detriot”, “San Diego”);

Page 9: Array and Hash Variables

4

9

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

Page 10: Array and Hash Variables

4

10

Using an Array in the Juniper Printers Script

• 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

Page 11: Array and Hash Variables

4

11

Using an Array in the Juniper Printers Script

• This code now includes declaring the @models array, as well as accessing and printing the corresponding model name

Page 12: Array and Hash Variables

4

12

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

Page 13: Array and Hash Variables

4

13

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

Page 14: Array and Hash Variables

4

14

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

Page 15: Array and Hash Variables

4

15

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”

Page 16: Array and Hash Variables

4

16

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

Page 17: Array and Hash Variables

4

17

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

Page 18: Array and Hash Variables

4

18

Using a Hash in the Juniper Printers Script

• 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}

Page 19: Array and Hash Variables

4

19

Using a Hash in the Juniper Printers Script

• This code now includes declaring the %systems hash, and printing out the full operating system name by accessing the %systems hash

Page 20: Array and Hash Variables

4

20

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

Page 21: Array and Hash Variables

4

21

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

Page 22: Array and Hash Variables

4

22

Modifying the Juniper Printers Form and Script

Page 23: Array and Hash Variables

4

23

The foreach and for Statements

• 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

Page 24: Array and Hash Variables

4

24

The foreach and for Statements

• 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

}

Page 25: Array and Hash Variables

4

25

The foreach and for Statements

• foreach– Example:

my ($num, @numbers);

@numbers = (5000, 200, 100, 3);foreach $num (@numbers) {

print “$num<BR>\n”;}

Result:

5000

200

100

3

Page 26: Array and Hash Variables

4

26

The foreach and for Statements

• 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

Page 27: Array and Hash Variables

4

27

The foreach and for Statements

• for:– Syntax:

for (initialization; loop condition; update) {one or more statements to be processed as long as the loop condition is true

}

– Example:my $num;for ($num = 1; $num < 4; $num = $num + 1) {

print “$num<BR>\n”;

}

Result:

1

2

3

Page 28: Array and Hash Variables

4

28

Updating the Juniper Printers script

• foreach will be used to process each member of the @sysletter array

Page 29: Array and Hash Variables

4

29

Summary• 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);

Page 30: Array and Hash Variables

4

30

Summary• 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);

Page 31: Array and Hash Variables

4

31

Summary

• 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

Page 32: Array and Hash Variables

4

32

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