Perl Variables: Array Web Programming
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
Array and List List Array 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
Array Syntax Array Name Array Elements array length 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
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 script Web Programming
Array Elements array element index of the last element in @array $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
Printing Arrays print @array; 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 script Web Programming
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 script Web Programming
Array Functions @array2 = sort (@array); @array2 = reverse (@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 script Web Programming
Array Functions Insert elements Replace elements Remove elements @array2 = splice (@array, offset, length); Remove elements (length from offset) @array2 holds the removed elements $array2 = splice (@array, offset, length); $array2 holds the last removed element @array2 = splice (@array, offset); Remove elements (from offset to end of the array) Insert elements splice (@array, offset, 0, LIST); Insert LIST at offset No return value Replace elements @array2 = splice (@array, offset, length, LIST); Remove elements (length from offset) & Insert LIST at offset Example script Web Programming
Reading Input into Arrays Reading from STDIN @array = <STDIN>; chomp @array; Example script Reading from a file reading line by line $line = <FILE>; reading all at once @lines = <FILE>; Example script Web Programming
Advanced I/O Command line arguments Working with a directory @ARGV holds command line arguments e.g. prog1.pl arg1 arg2 @ARGV holds (arg1, arg2) Example script 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