Perl Variables Array Web Programming 1 Review Perl
Perl Variables: Array Web Programming 1
Review: Perl Variables n n n Scalar ► e. g. $var 1 = “Mary”; $var 2= 1; ► holds number, character, string Array ► e. g. @array 1 = (“Mary”, ”Tom”); ► holds a list of scalars ► $array 1[0]=“Mary”; $array 1[1]=“Tom”; Associative array (i. e. hash) ► e. g. %hash 1 = (“Mary”, ”F”, ”Tom”, ”M”); ► holds key-value scalar pairs ► $hash{“Mary”}=“F”; $hash{“Tom”}=“M”; Web Programming 2
Array and List n List ► a sequence of scalar values (i. e. elements) • e. g. (1, 2, 3), (“a”, ”b”, ”c”), (1, ”a”, 2. 3, 4) n 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
Array Syntax n Array Name ► ► ► n 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” n array length ► ► number of elements in an array $array_length = @array; Web Programming 4
Array: Value Assignment n Assigning values to an array variable ► @array = (element 0, element 1, . . ); ► an element can be • a constant, a variable, an expression, or • a list range e. g. 1. . 5, “a”. . “z” ► e. g. • • • n @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 script Web Programming 5
Array Elements n array element ► n index of the last element in @array ► ► n $array[index] 1 less than array length $#array last element in @array ► ► ► $array[-1] $array[$#array] $array[@array-1] Web Programming 6
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 “$itemn”; } For ($i=0; $i <=$#array; $i++) { print “$array[$i]n”; } Example script Web Programming 7
String to Array Conversion n $string = join ($separator, @array); ► n $string = “@array”; ► n concatenates elements using $separator concatenates elements using “ ” @array = split (/$separator/, $string); ► splits $string by $separator into @array Example script Web Programming 8
Array Functions n @array 2 = sort (@array); ► n @array 2 = reverse (@array); ► n add $item to the end of @array chop (@array); ► n add $item to the start of @array push (@array, $item); ► n remove and return the last element of @array unshift (@array, $item); ► n remove and return the first element of @array $last = pop (@array); ► n reverses the order of array elements $first = shift (@array); ► n sorts alphabetically chops the last character of each element chomp (@array); ► removes the last character of each element if it is a newline character Example script Web Programming 9
Array Functions n Remove elements ► @array 2 = splice (@array, offset, length); • Remove elements (length from offset) • @array 2 holds the removed elements ► $array 2 = splice (@array, offset, length); • Remove elements (length from offset) • $array 2 holds the last removed element ► @array 2 = splice (@array, offset); • Remove elements (from offset to end of the array) n Insert elements ► splice (@array, offset, 0, LIST); • Insert LIST at offset • No return value n Replace elements ► @array 2 = splice (@array, offset, length, LIST); • Remove elements (length from offset) & Insert LIST at offset • @array 2 holds the removed elements Example script Web Programming 10
Reading Input into Arrays n Reading from STDIN ► @array = <STDIN>; chomp @array; Example script n Reading from a file ► reading line by line • $line = <FILE>; ► reading all at once • @lines = <FILE>; Example script Web Programming 11
Advanced I/O n Command line arguments ► @ARGV holds command line arguments • e. g. prog 1. pl arg 1 arg 2 @ARGV holds (arg 1, arg 2) Example script n Working with a directory ► Open a directory • opendir (IND, $directory); ► Read the contents. • @files = readdir (IND); ► Close the directory • closedir (IND); Example script Web Programming 12
- Slides: 12