
Lecture #7 Perl List, Array, and Hash What is a list? A Perl list is simply a collection of scalar values, separated by a comma (,) in a pair of parentheses. Items in a list do not have to be the same kind. They could be a mix of numbers and strings. The following is an example of list whose elements are all integers. (3, 8, 6, 7, 0, 5, 4, 9, 1, 2) The following is sample list of strings. ("Pomona", "Long Beach", "West Hills", "Irvine"); The following is a list of mixed numbers and strings. (245, "Perl Programming", 246, "PHP Programming", 247, "Python Programming"); When all elements in a list are consecutive numbers or letters, you can use the range operator (..) to represent them. (1 .. 100) (A .. Z) The following is a complete code that demonstrates how to use the range operator to define Perl lists. It also uses foreach loops to iterate through lists. By the way, the lc() function change every letter to lower case. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; foreach $c (A .. Z) { print lc($c), "<br>"; } foreach (0..9) { print $_, "<br>"; } print end_html; What is An array is group of items that have something in common, such as Spring, Summer, Fall, and an Array? Winters. They are four individual items; however, they are also one of the seasons. You can, therefore, group these four items into an array called season. Items in an array may have sequence as well. Spring, for instance, come before Summer, followed by Fall, and then Winter. Most programming books refer items in an array as “elements” or “components”. All items in a Perl arrays are placed in sequence. Sequence of items in array is in an order known as “index” or “key”. The official syntax to declare a Perl array is to declare a variable which is prefixed by an “@” sign. The variable, thus, becomes the identifier (name) of the array. The following depicts the syntax: 192 @arrayName Elements (items) of the array is populated, one by one, using the following syntax, where index is an integer starting at 0. $arrayName[index] = "value" The following is a complete script that illustrates how to declare a string array and then add four elements to it. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @season; $season[0] = "Fall"; $season[1] = "Winter"; $season[2] = "Spring"; $season[3] = "Summer"; print end_html; Similarly, the following declare a numeric array named “x”, and then populate it with 4 elements: 8, 6, 4, and 2. Their indexes are 0, 1, 2, and 3 respectively. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @x; $x[0] = 8; $x[1] = 6; $x[2] = 4; $x[3] = 2; print end_html; In Perl, there is a shorthand way to create arrays. You can declare an array by first creating a list and then assigning the entire list to a variable which is prefixed by an “@” sign. The variable, thus, becomes the identifier (name) of the array. The following depicts the syntax: @arrayName = (elements); The following example creates a string array named “season” and a numeric array named “x”. The “season” array has four elements: “Spring”, “Summer”, “Fall”, and “Winter”. The “x” array has 10 elements. Both arrays are assigned a Perl list of items. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @season = ("Spring", "Summer", "Fall", "Winter"); @x = (4, 7, 5, 8, 2, 0, 1, 9, 3, 6); 193 print end_html; An array must contain at least two items; therefore, arrays are not scalar. In order to distinguish an array from a scalar variable, which is prefixed with a dollar sign ($), Perl uses an “at” sign (@) to denote the beginning of array name. Many programming languages, such as C++, C#, and Java, do not permit an array to have elements of different types. Perl, does not have such limit. Elements in a Perl array can be a mix of numbers and strings. The following demonstrates how to create a Perl array of different types. After all, a Perl array is simply a grouped list of scalar variables. Each element in the array is an individual value. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @a; # declare $a[0] = "Apple"; # populate $a[1] = 6.1; $a[2] = 'k'; $a[3] = 2; print end_html; Identifying The following is the syntax for identifying the nth value of a given array: elements in an Array $arrayName[n-1] According to the following statement, the first value of the season array is represented by $season[0], the second is $season[1], the third is $season[2], and the forth is $season[3]. The number inside square brackets is sometimes referred to as the key or index. @season=("Spring", "Summer", "Fall", "Winter"); In the following example, the print $season[2]; line tells the computer to display the third item, which is Fall. For example: #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @season=("Spring", "Summer", "Fall", "Winter"); print $season[2]; print end_html; The output in a Web browser is: The ouput of the following is: k. 194 #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @a; $a[0] = "Apple"; $a[1] = 6.1; $a[2] = 'k'; $a[3] = 2; print $a[2]; print end_html; You can assign a new value to an existing item of an array. Each element in an array is a scalar variable; therefore, they behave in the same manner as any scalar variable. You just have to stick to the itemization to specify which item is the one to be assigned a new value. In the following example, the $weekdat[0]=”Monday”; line tells the computer to assign a new value “Monday” to the first element as an replacement of “Mon”, thus the print $weekday[0]; line will display Monday on the screen. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @weekday=("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); print $weekday[0], "<br>"; $weekday[0]="Monday"; print $weekday[0], "<br>"; print end_html; The output in a browser is : Repetition structures, such as the for loop, are useful structures to iterate through a Perl array to retrieve elements. The following example illustrates how to use a for loop for this purpose. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @list = ("Apple", 169.75, "student", 31); $size = @list; # get total number of elements for ($i=0; $i < $size; ++$i) { print "$i $list[$i]", br; 195 } print end_html; The output looks: 0 Apple 1 169.75 2 student 3 31 In Perl, you can simply create a variable and assign the array to it. Perl will then store the “size” of array in the variable. The “size” of the array is the total number of elements the array has. Some books refer the term “size” to “length”. $size = @weekday; Another way to find the size of an array is to call the scalar() function with the following syntax. scalar(@arrayName) The following is another example that uses a for loop to iterate through the entire “weekday” array and print each element one by one. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @weekday=("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); $size = @weekday; #find the size of array for ($i=0; $i<$size; $i++) { print $weekday[$i], br; } print end_html; You can also use the while loop to replace the research the same goal. For example, you can rewrite the above script to: #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @list=("Apple", 169.75, "student", 31); $size = @list; $i=0; while ($i < $size) { print "$i $list[$i]", br; $i++; } print end_html; 196 The foreach loop is a repetition frequently used to process Perl lists and hashes. Similar to the for loop, foreach steps through each element of an array using an iterator. Rather than using a scalar as that iterator (like the for loop), foreach uses the array itself. In the following example, $c is a variable that will represent the value in an iteration. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @car=("Toyota", "Nissan", "Mazda"); foreach $c (@car) { print $c, br; } print end_html; The output looks: Toyota Nissan Mazda The following example declares a scalar variable $n as the iterator, which will take turn to represent each value of the array. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @number=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); foreach $n (@number) { $n = $n*2; print $n, " "; } print end_html; The $n = $n*2; line tells the computer to multiple the current value with 2 and then use the product as new value. Consequently the output will look like this: 2 4 6 8 10 12 14 16 18 20 Here is another example. The foreach serves as an iterator to step through each element of the “weekday” array. The Perl $_ variable is known as the “default input and pattern matching space”. $_ is a special variable which contains the value of the last matched expression; therefore, it can be used to represent the element that was just retrieved. #!"X:\xampp\perl\bin\perl.exe" use CGI qw(:standard); print header, start_html; @weekday = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); foreach (@weekday) 197 { print $_, br; } print end_html; Here is another example which retrieves the current “day of week” value from the localtime() function, $dt[6].
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages33 Page
-
File Size-