Professional Documents
Culture Documents
What is Perl?
Perl is a Portable Scripting Language No compiling is needed. Runs on Windows, UNIX, LINUX and cygwin Fast and easy text processing capability Fast and easy file handling capability
Learning Perl
By Larry Wall Published by O'Reilly By Larry Wall,Tom Christiansen and Jon Orwant Published by O'Reilly
Programming Perl
Web Site
http://safari.oreilly.com
Run it as follows:
to find Perl -w switches on warning : not required but a really good idea
Numerical Literals
Numerical Literals
Integer Floating Point Scientific Notation Scientific Notation Underscores instead of commas for long numbers
String Literals
String Literals
There is more than one way to do it! 'Just don't create a file called -rf.' Beauty?\nWhat's that?\n Real programmers can write assembly in any language.
Types of Variables
Types of variables: Scalar variables : $a, $b, $c Array variables : @array Hash variables : %hash File handles : STDIN, STDOUT, STDERR Variables do not need to be declared Variable type (int, char, ...) is decided at run time $a = 5; # now an integer
$a = perl;
# now a string
11
12
$language = Perl; if ($language == Perl) ... # Wrong! if ($language eq Perl) ... #Correct
13
Numeric : >
String : gt
Numeric : >=
Numeric : < Numeric : <=
String : ge
String : lt String : le
Less than
14
String Functions
Convert to upper case
$name = uc($name);
$name = ucfirst($name);
$name = uc($name); Convert only the first char to upper case $name = ucfirst($name);
$name = lc($name); Convert only the first char to lower case $name = lcfirst($name);
#!/usr/bin/perl $var1 = larry; $var2 = moe; $var3 = shemp; Output: Larry, MOE, sHEMP
16
17
Variable Interpolation
Perl looks for variables inside strings and replaces
Character Interpolation
List of character escapes that are recognized
\n \t \r
Common Example :
# a number prints 22 looks like a string, but ... # will print 40!
20
statements.
unless ($weather eq Rain) { print Dress as you wish!\n; } else { print Umbrella!\n; }
While Loop
Example : $i = 0; while ( $i <= 1000 ) { print $i\n; $i++; }
24
Until Loop
The until function evaluates an expression
For Loops
Answer
0 2 4
Exercise
Use a loop structure and code a program that
Exercise
#! /usr/bin/perl for ($i=0, $j=0; $i<100; $i++) { if ( $j==3){$chain.=B;$j=0;} else {$chain.=A; $j++;} print $chain\n; }
30
for every patient. You want to generate an artificial random study for 100 patients:
Patient 1 99 Patient 2 65 Patient 3 89 . Tip: - use the srand to seed the random number generator -use rand 100 to generate values between 0 and 100 : rand 100
31
Exercise
for ($i=0; $i<100; $i++) { $v=rand 100; #print Patient $i $v\n; printf Patient %d %.2f\n\n, $i, $v; #%s : chaines, strings #%d : integer #%f : floating points }
32
Arrays
Array variable is denoted by the @ symbol @array = ( Larry, Curly, Moe ); To access the whole array, use the whole
array
Notice that you do not need to loop through the whole array to print it Perl does this for you
34
Arrays cont
Array Indexes start at 0 !!!!!
To access one element of the array : use $ Why? Because every element in the array is scalar
Question:
35
array
print $#array; # prints 2 in the previous # example
Note another way to find the number of
Sorting Arrays
Perl has a built in sort function Two ways to sort: Default : sorts in a standard string comparisons order sort LIST Usersub: create your own subroutine that returns an integer less than, equal to or greater than 0 Sort USERSUB LIST The <=> and cmp operators make creating sorting subroutines very easy
37
39
Foreach
Foreach allows you to iterate over an array Example: foreach $element (@array) { print $element\n; } This is similar to : for ($i = 0; $i <= $#array; $i++) { print $array[$i]\n; }
40
Use the following initialization to sort individuals by age and then by income: Syntax
@sortedArray = sort numeric @unsortedArray; sub numeric { return $a <=> $b; } Data
Output:
Name X Age A Income I Tip: -Sort the index, using information contained in the other arrays.
42
@index=(0,1,2,3,4,5); @name=(V,W,X,Y,Z); @age=(10,20, 15, 20, 10); @income=(100,670, 280,800,400); foreach $i ( sort my_numeric @index) { print $name[$i] $age[$i] $income[$i]; } sub my_numeric { if ($age[$a] == $age[$b]) {return $income[$a]<=>$income[$b]; } else {return $age[$a]<=>$age[$b]; } }
43
Manipulating Arrays
previously
Split into characters @stooge = split( //, curly ); # array @stooge has 5 elements: c, u, r, l, y
45
Split cont..
Split on any character @array = split( /:/, 10:20:30:40); # array has 4 elements : 10, 20, 30, 40 Split on Multiple White Space @array = split(/\s+/, this is a test; # array has 4 elements : this, is, a, test
46
Arrays to Strings
Array to space separated string @array = (Larry, Curly, Moe); $string = join( ;, @array); # string = Larry;Curly;Moe Array of characters to string @stooge = (c, u, r, l, y); $string = join( , @stooge ); # string = curly
47
48
To remove the last element of the array (LIFO) $elment = pop @array; print $element; # prints Shemp @array now has the original elements (Larry, Curly, Moe)
49
50
Exercise: Spliting
Instructions Remove
Add
The enquiry 1 was administered to five couples The enquiry 2 was administered to six couples The enquiry 3 was administered to eigh couples Into five couples were administered the enquiry 1 .
51
Exercise: Spliting
Use split, shift and push to turn the following string:
$s[0]= The enquiry 1 was administered to five couples; $s[1]= The enquiry 2 was administered to six couples; $s[2]= The enquiry 3 was administered to eigh couples; foreach $s(@s) { @s2=split (/was administered to/, $s); $new_s=$s2[1] were admimistered $s2[0]; print $new_s\n; }
52
Multidimentional Arrays
@tab=([Monday,Tuesday], [Morning,Afternoon,Evening]); $a=$tab[0][0] # $a == Monday $tab2=(midnight, Twelve); $tab[2]=\@tab2 # integrate tab2 as the last row of tab
54
Thank you