Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.

Similar presentations


Presentation on theme: "Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar."— Presentation transcript:

1 Chapter 17 Arrays Perl uses @ to denote an array, for example: @x = (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 = @x;.

2 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

3 Chapter 17 Logical Operators Operator Meaning ! not && and || or The logical operators are arranged in order of precedence.

4 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

5 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.

6 Chapter 17 The for Statement for ($i=0; $i < $size; $i++) { print "$barnyard[$i] \n";} @barnyard = ("pig", "ox", "horse", "chicken"); $size = @barnyard; Creating the barnyard array. Print the array with a for statement.

7 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.

8 Chapter 17 The while Statment $i = 0; while ($i < $size){ print "$barnyard[$i] \n"; $i++; } Printing the barnyard array with a while statement.

9 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.

10 Chapter 17 The until Statment $i = 0; until ($i == $size){ print "$barnyard[$i] \n"; $i++; } Printing the barnyard array with an until statement.

11 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.

12 Chapter 17 The foreach Statment foreach $animal(@barnyard) { print "$animalBR>\n";} Printing the barnyard array with a foreach statement.

13 Chapter 17 Perl Array Functions Function Meaning @ name = sort{ order } opt @ array Sort array. @ name = reverse @ array Reverse array. @ name = @ array [ start…stop ] Slice array @ name = @ array [ i, j, k ] Slice array. @ name = grep(/ pattern /,@ array ) Pattern Match.. push(@ array, $ elem ) Add to right end. unshift(@ array, $ elem ) Add to left end. $ name = pop @ array Remove from right end. $ name = shift@ array Remove from left end.

14 Chapter 17 References A reference is a scalar that holds the address of a variable. References

15 Chapter 17 References To create a reference, use \ : @x_ref = \$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.

16 Chapter 17 Matrices @matrix = ( [1,2], [3,4], [5,6]); A matrix (or two-dimensional array) is an array that contains references to one dimensional arrays as in: @row0 = (1,2); @row1 = (3,4); @row2 = (5,6); @matrix = (\@row0, \@row1, \@row2); Anonyomous references Named references

17 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.

18 Chapter 17 Creating Hashes %animals = (“dogs”, “Hunde”, “cats”, “Katzen”); %animals = (“dogs”=> “Hunde”, “cats”=> “Katzen”); Either of these techniques will create a hash.

19 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”};

20 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.

21 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”;}

22 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.

23 Chapter 17 An Array of Hashes @numbers = ( {#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.

24 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.

25 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};


Download ppt "Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar."

Similar presentations


Ads by Google