320 likes | 447 Views
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.
E N D
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
Introduction • Perl has 3 data types for variables: • scalar • Can store 1 value • array • Can store multiple values • hash • Can store multiple values
Juniper Printers Script • Planning and Coding: • Output:
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
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
Array Variables • Array declaration examples: • my @sales = (25000, 35000, 10000); • my @cities = (“Boston”, “Chicago”, “Detriot”, “San Diego”);
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
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
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
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
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
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
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”
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
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
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}
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
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
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
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
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 }
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
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
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
Updating the Juniper Printers script • foreach will be used to process each member of the @sysletter array
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: myarrayname = (list);
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: myhashname = (key1, value1, key2, value2, ... keyn, valuen);
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
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