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

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
The Web Warrior Guide to Web Design Technologies
Creating PHP Pages Chapter 7 PHP Decisions Making.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
JavaScript, Fourth Edition
JavaScript, Third Edition
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
9 1 DBM Databases CGI/Perl Programming By Diane Zak.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
PHP Constructs Advance Database Management Systems Lab no.3.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
JavaScript, Fourth Edition
CPTG286K Programming - Perl Chapter 4: Control Structures.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
JavaScript, Sixth Edition
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
PHP using MySQL Database for Web Development (part II)
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
Chapter 5 - Control Structures: Part 2
The Selection Structure
JavaScript Syntax and Semantics
Scripts & Functions Scripts and functions are contained in .m-files
The structure of computer programs
Microsoft Visual Basic 2005 BASICS
Arrays, For loop While loop Do while loop
Chapter 10 Programming Fundamentals with JavaScript
WEB PROGRAMMING JavaScript.
Chapter (3) - Looping Questions.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
3 Control Statements:.
Chapter 8: More on the Repetition Structure
Fundamentals of visual basic
The Selection Structure
CIS 136 Building Mobile Apps
Presentation transcript:

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

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

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

4 4 Juniper Printers Script Planning and Coding: Output:

4 5 Juniper Printers Script

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

4 7 Array Variables Array declaration syntax: –my arrayname=(list); Name of array must begin with at sign After the name must start with a letter, and then a combination of letters, numbers, or underscores The list consists of values separated by commas

4 8 Array Variables Array declaration examples: = (25000, 35000, 10000); = (“Boston”, “Chicago”, “Detriot”, “San Diego”);

4 9 Array Variables Accessing the array –Replace in the array name with a $, array name, index enclosed in square brackets ([ ]) –Example: = (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

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 array

4 11 Using an Array in the Juniper Printers Script This code now includes declaring array, as well as accessing and printing the corresponding model name

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

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

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

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”

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} = $sales{Feb} = $sales{Mar} = 10000

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

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}

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

4 20 Modifying the Juniper Printers Form and Script HTML checkboxes: –Use the same key, in this form, System –Syntax: 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

4 21 Modifying the Juniper Printers Form and Script One change made involves storing the System key array instead of $sysletter, due to the possibility of multiple values for that key

4 22 Modifying the Juniper Printers Form and Script

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

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 }

4 25 The foreach and for Statements foreach –Example: = (5000, 200, 100, 3); foreach $num { print “$num \n”; } Result:

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

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 \n”; } Result: 1 2 3

4 28 Updating the Juniper Printers script foreach will be used to process each member of array

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

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, 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);

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

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