Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar notation, for example, $x[0]. To find the size of an array, assign it to a scalar, for example $size
Chapter 17 Relational Operators Numeric String Meaning < lt less than <= le less than or equal > gt greater than >= ge greater than or equal == eq equal != ne not equal cmp signed comparison Perl uses separate operators for numbers and strings
Chapter 17 Logical Operators Operator Meaning ! not && and || or The logical operators are arranged in order of precedence.
Chapter 17 Iteration Statments Perl contains four iteration statements. Each requires a block of code for the body. The statements are: The for Statement The while Statement The until Statement The foreach Statement
Chapter 11 The for Statement The for statement is typically used for counting. The structure of the statement is: for ( InitialExpression ; Test Expression ; Change Expression ) { Statement; } Braces are required in Perl.
Chapter 17 The for Statement for ($i=0; $i < $size; $i++) { print "$barnyard[$i] = ("pig", "ox", "horse", "chicken"); $size Creating the barnyard array. Print the array with a for statement.
Chapter 11 The while Statement The while statement is used for error loops or to process the key-value pairs in a hash. The structure of the statement is: while ( Expression ) { Statement; } Braces are required in Perl.
Chapter 17 The while Statment $i = 0; while ($i < $size){ print "$barnyard[$i] \n"; $i++; } Printing the barnyard array with a while statement.
Chapter 11 The until Statement The until statement executes the code in the body until the expression becomes true. This statement is unique to Perl. Its structure is: until ( Expression ) { Statement; } Braces are required in Perl.
Chapter 17 The until Statment $i = 0; until ($i == $size){ print "$barnyard[$i] \n"; $i++; } Printing the barnyard array with an until statement.
Chapter 11 The foreach Statement The foreach statement automates array processing. This statement is also unique to Perl. Its structure is: foreach $ variable array ) { Statement; } Braces are required in Perl.
Chapter 17 The foreach Statment foreach { print "$animalBR>\n";} Printing the barnyard array with a foreach statement.
Chapter 17 Perl Array Functions Function name = sort{ order } array Sort name = array Reverse name array [ start…stop ] Slice name array [ i, j, k ] Slice name = grep(/ pattern array ) Pattern Match.. array, $ elem ) Add to right end. array, $ elem ) Add to left end. $ name = array Remove from right end. $ name = array Remove from left end.
Chapter 17 References A reference is a scalar that holds the address of a variable. References
Chapter 17 References To create a reference, use \ = \$x; To dereference, that is, to obtain the contents of the “pointed to” variable, use $. For example, print “$$x_ref”; prints the contents of $x not the address.
Chapter 17 = ( [1,2], [3,4], [5,6]); A matrix (or two-dimensional array) is an array that contains references to one dimensional arrays as = = = = Anonyomous references Named references
Chapter 17 Hashes A hash uses a randomizing function to store key-value pairs. Perl uses % to denote a hash. Hash elements are accessed by their keys. Scalar notations is used. There are two forms for creating a hash.
Chapter 17 Creating Hashes %animals = (“dogs”, “Hunde”, “cats”, “Katzen”); %animals = (“dogs”=> “Hunde”, “cats”=> “Katzen”); Either of these techniques will create a hash.
Chapter 17 Hash Operations $animals{“horses”} = “Pherde”; $german = $animals{“dogs”}; Adds horses to the hash. Extracts the german word for dogs. Deletes dogs from the hash. delete $animals{“dogs”};
Chapter 17 Printing the Keys in a Hash foreach $key = (keys %animals) { print “$key \n”;} To print all of the keys in a hash,we use the keys function.
Chapter 17 Printing the Values in a Hash To print all of the values in a hash,we use the values function. foreach $value = (values %animals) { print “$value \n”;}
Chapter 17 Printing the Key-Value Pairs while(($english, $german) = each (%animals)) { print “$english = $german \n”;} To print all of the key-value pairs we use the each function and a while statement.
Chapter 17 An Array of = ( {#0 english => “zero”, german => “null”,}, ); An array of hashes is an array where each element is a hash. The array is created as a list. Each element is a hash, so we use hash notation.
Chapter 17 An Hash of Hashes %weekdays = (‘Monday’ => { german => “Montag”, tibetan => “sa da wa”,}, ); An hash of hashes is a hash where the values are hashes.
Chapter 17 Accessing Advanced Structures To access an array of hashes, we use an index and a key, for example: $english = $numbers[0]{english}; To access a hash of hashes, we use two keys, for example: $german = $weekday {‘Monday’}{german};