Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.

Similar presentations


Presentation on theme: "Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g."— Presentation transcript:

1 Perl Variables: Array Web Programming1

2 Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g. @array1 = (“Mary”,”Tom”); ► holds a list of scalars ► $array1[0]=“Mary”; $array1[1]=“Tom”; Associative array (i.e. hash) ► e.g. %hash1 = (“Mary”,”F”,”Tom”,”M”); ► holds key-value scalar pairs ► $hash{“Mary”}=“F”; $hash{“Tom”}=“M”; Web Programming 2

3 Array and List List ► a sequence of scalar values (i.e. elements) e.g. (1, 2, 3), (“a”, ”b”, ”c”), (1, ”a”, 2.3, 4) Array ► a Perl variable that holds a list data e.g. @array = (1, 2, 3); ► an array element is a scalar variable e.g. $array[0]; Web Programming 3

4 Array Syntax Array Name ► first character must be @. ► second character must be a letter. ► can consist of only letters, digits, or underscore ► case-sensitive ► can be as long as you want (within reason) Array Elements ► $array_name[$index] ► array index goes from 0 to (array length -1) e.g. @array = (“cat”, ”dog”, “pig”); $array[0] is “cat”, $array[1] is “dog”, $array[2] is “pig” array length ► number of elements in an array ► $array_length = @array; Web Programming 4

5 Array: Value Assignment Assigning values to an array variable ► @array = (element0, element1,..); ► an element can be a constant, a variable, an expression, or a list range e.g. 1..5, “a”..“z” ► e.g. @array = (“one”, 2, “three”, “four”, 5) @array = (1, $a, “two”, @b) @array = (1, 1+1, $a.$b, 1..5) @array = (1) @array = ( ) Array values in a conditional expression ► if (@array): false if a null array, true otherwise  Example scriptExample script Web Programming 5

6 Array Elements array element ► $array[index] index of the last element in @array ► 1 less than array length ► $#array last element in @array ► $array[-1] ► $array[$#array] ► $array[@array-1] Web Programming 6

7 Printing Arrays ► print @array; prints all elements concatenated together. ► print “@array”; prints elements separated by a space (default list separator). the list separator ($”) can be set to a different value. ► foreach $item (@array) { print “$item\n”; } ► For ($i=0; $i <=$#array; $i++) { print “$array[$i]\n”; }  Example scriptExample script Web Programming 7

8 String to Array Conversion $string = join ($separator, @array); ► concatenates elements using $separator $string = “@array”; ► concatenates elements using “ ” @array = split (/$separator/, $string); ► splits $string by $separator into @array  Example scriptExample script Web Programming 8

9 Array Functions @array2 = sort (@array); ► sorts alphabetically @array2 = reverse (@array); ► reverses the order of array elements $first = shift (@array); ► remove and return the first element of @array $last = pop (@array); ► remove and return the last element of @array unshift (@array, $item); ► add $item to the start of @array push (@array, $item); ► add $item to the end of @array chop (@array); ► chops the last character of each element chomp (@array); ► removes the last character of each element if it is a newline character  Example scriptExample script Web Programming 9

10 Reading Input into Arrays Reading from STDIN ► @array = ; chomp @array;  Example scriptExample script Reading from a file ► reading line by line $line = ; ► reading all at once @lines = ;  Example scriptExample script Web Programming 10

11 Advanced I/O Command line arguments ► @ARGV holds command line arguments e.g. prog1.pl arg1 arg2 @ARGV holds (arg1, arg2)  Example scriptExample script Working with a directory ► Open a directory opendir (IND, $directory); ► Read the contents. @files = readdir (IND); ► Close the directory closedir (IND);  Example scriptExample script Web Programming 11


Download ppt "Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g."

Similar presentations


Ads by Google