Perl Programming
Perl Crash Course | Perl Crash Course |
|
|
This tutorial is also available for download as PDF document As you might notice we've defined array with @ but printed a single value of that array using $. This is correct because we want to print a single value. Please note that $#array1 in our example is number of elements, but because elements from an array in Perl starts with value 0, the real number of elements of an array is $#array + 1. There is another method to define an array: @array2 = qw(Value1 Value2 Value3 Value4); Some functions for working with arrays: - pop - remove last element of an array: - push - add an element to the end of array; - shift - removes first element of an array; - unshift - add an element to the beginning of array; - sort - sort an array. Let's see some examples. Pop Function (remove last element of an array): #!/usr/bin/perl -w @array1 = ("Data1", "Data2", "Data3"); print "Array1 values: @array1[0..$#array1]\n"; pop @array1; print "Array1 after applying pop function: @array1[0..$#array1]\n"; Push Function (add an element to the end of array): #!/usr/bin/perl -w @array1 = ("Data1", "Data2", "Data3"); print "Array1 values: @array1[0..$#array1]\n"; push @array1, "Data4"; print "Array1 after applying push function: @array1[0..$#array1]\n"; Shift Function (removes first element of an array): #!/usr/bin/perl -w @array1 = ("Data1", "Data2", "Data3"); print "Array1 values: @array1[0..$#array1]\n"; shift @array1; print "Array1 after applying shift function: @array1[0..$#array1]\n"; The same principle apply for unshift and sort functions. Sort functions works best with strings. 2.3 Hashes -------------- Hashes are types of variables defined as key / value pair. Example of defining a hash variable: %name_email = ("John", " \n This email address is being protected from spam bots, you need Javascript enabled to view it , "George", " \n This email address is being protected from spam bots, you need Javascript enabled to view it "); Another way to define a has variable: %name_email = ( John => " \n This email address is being protected from spam bots, you need Javascript enabled to view it ", George => " \n This email address is being protected from spam bots, you need Javascript enabled to view it ", ); Example of using hash variables: #!/usr/bin/perl -w %name_email = ( "John", " \n ", "George", " \n "); print $name_email{"John"}; Note: We've used escape character to preserver @. Also note that printing a hash variable means to print a scalar with value key between braces { }. 3. Perl control structures ------------------------- 3.1 Conditionals ---------------- For testing conditionals within Perl if it is used. To better illustrates, see the following example: #!/usr/bin/perl -w $var1 = 100; $var2 = 200; if ($var1 < $var2) { print "$var1 < $var2\n"; } Note1: When evaluating expressions if variables are numbers we will use mathematical operators ( < > = <= >= ==). When we use string variables we use string evaluation operators like gt (greater then) eq (equal) and so on. Note 2: When we evaluate two numbers to be identical, we use == operator (not = which is used for assigning values. Another example follows: #!/usr/bin/perl -w $var1 = 400; $var2 = 200; if ($var1 < $var2) { print "$var1 < $var2\n"; } elsif ($var1 > var2) { print "$var1 > $var2\n"; } elsif function as a nested if. The inverse test of if is unless function: unless ($var1 == $var2) { print "$var1"; 3.2 Loops ---------- 3.2.1 For loops --------------- In Perl sometimes are many way to solve a problem. We will show 3 ways to construct a loop using for. Example 1: For loop using C style: #!/usr/bin/perl -w # for loop example 1 for ($i = 1; $i < 100; $i++) { print "$i\n"; } Example 2: for loops using ranges #!/usr/bin/perl -w # for loop example 2 $var1 = 1; $var2 = 100; $i = 1; for ($var1..$var2) { print "$i\n"; $i+=1; } Example 3: loop using foreach #!/usr/bin/perl -w # for loop example 3 @array1 = ( "Val1", "Val2", "Val3", "Val4", "Val5"); foreach (@array1) { print "$_\n"; } Note1: $_ will print the current value of an array. 3.2.2 While loops ---------------------- An example is presented next: #!/usr/bin/perl -w $var1 = 1; $var2 = 8; while ($var1 < $var2) { print "$var1\n"; $var1 += 1; } 3.2.3 Until loops -------------------- Until is negation of while. Here is an example: #!/usr/bin/perl -w $var1 = 1; $var2 = 8; until ($var2 < $var1) { print "$var2\n"; $var2 -= 1; } 4. Defining and using subroutines ------------------------------------------ Subroutines allow us to better structure our code, organize it and reuse it. A subrutine will start with keyword sub. The following example shows how to define a subroutine which calculates sum of two numbers: #!/usr/bin/perl -w $var1 = 100; $var2 = 200; $result = 0; $result = my_sum(); print "$result\n"; sub my_sum { $tmp = $var1 + $var2; return $tmp; } Note: Subroutines might have parameters. When passing parameters to subroutines, it will be stored in @_ array. Do not confuse it with $_ which stores elements of an array in a loop. 5. Using file parameters (positional parameters) ------------------------------------------------------------ Sometimes we need to transmit parameters to our script files. @ARGV is an array reserved for parameters transmitted to files (default value of number of arguments is set -1 if no parameters are transmitted. #!/usr/bin/perl -w if ($#ARGV < 2) { print "You must have at least 3 parameters.\n"; } else { print "Your parameters are: @ARGV[0..$#ARGV]\n"; } |
| < Prev |
|---|
