Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Variables: Array Web Programming.

Similar presentations


Presentation on theme: "Perl Variables: Array Web Programming."— Presentation transcript:

1 Perl Variables: Array Web Programming

2 Review: Perl Variables
Scalar e.g. $var1 = “Mary”; $var2= 1; holds number, character, string Array = (“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

3 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 = (1, 2, 3); an array element is a scalar variable e.g. $array[0]; Web Programming

4 Array Syntax Array Name Array Elements array length
first character must 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) = (“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 Web Programming

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 , “a”..“z” e.g. @array = (“one”, 2, “three”, “four”, 5) @array = (1, $a, @array = (1, 1+1, $a.$b, 1..5) @array = (1) @array = ( ) Array values in a conditional expression if false if a null array, true otherwise Example script Web Programming

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

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

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

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

10 Array Functions Insert elements Replace elements Remove elements
@array2 = splice offset, length); Remove elements (length from offset) @array2 holds the removed elements $array2 = splice offset, length); $array2 holds the last removed element @array2 = splice offset); Remove elements (from offset to end of the array) Insert elements splice offset, 0, LIST); Insert LIST at offset No return value Replace elements @array2 = splice offset, length, LIST); Remove elements (length from offset) & Insert LIST at offset Example script Web Programming

11 Reading Input into Arrays
Reading from STDIN @array = <STDIN>;  Example script Reading from a file reading line by line $line = <FILE>; reading all at once @lines = <FILE>; Example script Web Programming

12 Advanced I/O Command line arguments Working with a directory
@ARGV holds command line arguments e.g. prog1.pl arg1 arg @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


Download ppt "Perl Variables: Array Web Programming."

Similar presentations


Ads by Google