0% found this document useful (0 votes)
14 views125 pages

Perl Training Session1 22nd Sept 2012

The document provides an agenda and introduction for a Perl Basics training session. The training will cover the basic features, syntax, input/output, scalars, strings, control flow, arrays, hashes, and combining Unix commands in Perl. It includes examples of basic syntax like variables, user input, strings, and command substitution. The goal is to teach the fundamentals of the Perl programming language.

Uploaded by

Amena Farhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views125 pages

Perl Training Session1 22nd Sept 2012

The document provides an agenda and introduction for a Perl Basics training session. The training will cover the basic features, syntax, input/output, scalars, strings, control flow, arrays, hashes, and combining Unix commands in Perl. It includes examples of basic syntax like variables, user input, strings, and command substitution. The goal is to teach the fundamentals of the Perl programming language.

Uploaded by

Amena Farhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 125

Perl BasicsTraining

(Session 1)

Arpit Seksaria
22nd Sept, 2012

22/09/2012 Mirafra Confidential 1


Agenda

 To introduce the features provided by Perl


 To learn the basic syntax & simple Input/Output control in Perl
 To learn about:
 Scalar variables

 Strings

 Control flow

 Arrays & lists

 Perl hashes

 And more ….

 To learn how to combine Unix Command in Perl using backquotes


 Demo
 Labs

22/09/2012 Mirafra Confidential 2


Introduction (1)

 PERL- Practical Extraction Report Language.


 Developed by Larry Wall.
 Perl is a scripting language that makes manipulation of
text, files and processes easy.
 Perl was originally designed under Unix, but now also
runs under all operating systems (including Windows).

22/09/2012 Mirafra Confidential 3


Introduction (2)

 More concise and readable.


 Perl has replaced shell programming as the most popular
programming language for text processing and Unix
system administration.
 Many libraries available (e.g. database, internet)
 Perl is also a popular language for CGI (for web server)
and GUI programming.

22/09/2012 Mirafra Confidential 4


Executing a Perl program

 You can run the script directly if you make the script
executable, and the first line is of the following form
( #!/usr/... must start from the first column):

$ chmod +x simple.pl
$ cat simple.pl
#!/usr/local/bin/perl5 -w
$ ./simple.pl (or)
$ perl simple.pl (executing script using this, doesn’t require the perl interpreter path)

22/09/2012 Mirafra Confidential 5


Basic Syntax (1)

 The -w option tells Perl to produce extra warning


messages at run time.
#!/usr/local/bin/perl5 –w (or)
#!/usr/local/bin/perl5
use warnings;
 To warn at compile time by -c option.
Inside file : use strict
Compile as : perl -c filename
 Whitespace doesn't matter in Perl, except for
#!/usr/local/bin/perl5 -w on column1, line1.

22/09/2012 Mirafra Confidential 6


 Perl is like a compiler and an interpreter.
 It's a compiler because the program is completely read
and parsed before the first statement is executed.
 It's an interpreter because there is no object code sitting
around filling up disk space.

22/09/2012 Mirafra Confidential 7


Basic Syntax (2)

 All perl statements must end in a semicolon ; (like C++)


except the last line.
 In Perl, comments begin with # (like shell scripts)
 everything after the # to the end of the line is ignored.
 # need not be at the beginning of the line.
 there are no C++-like multiline comments: /* */
 But you do have =head/cut which works the same

22/09/2012 Mirafra Confidential 8


Perl Example

 Here is a “hello world” Perl program:

#!/usr/local/bin/perl5 -w
print "Hello world\n";

 The print command sends the string to the screen, and “\n” adds
a newline.
 You can optionally use parenthesis around the argument in print:
print ("Hello world\n");

22/09/2012 Mirafra Confidential 9


Scalar Variables

 A scalar variable can hold a single number or string (like


shell variables), including integers and floating-point
numbers (unlike shell variables).

 Scalar variables begin with $ followed by a letter, and


then possibly more letters, digits, or underscores. (e.g.,
$n, $n1, $name, $first_name).

 Scalar variables are case sensitive.

22/09/2012 Mirafra Confidential 10


Assigning Scalar Variables

 Scalars are assigned using “=“


$scalar = expression;

 To assign a value to a scalar variable:


$number = 25;
$name = "Bill Clinton";

 Unlike shell scripts, use “$”, both when the variable is


used and assigned.

22/09/2012 Mirafra Confidential 11


Numerical Scalar Variables

 Internally, all numerical scalar values are stored as floats


(so you don’t have to worry about integer division in Perl
like you do in C++).
 Perl supports the usual C++ numerical operations:
$a = 25; # $a is now 25
$a += 5; # $a is now 30
$a *= 3; # $a is now 90
$a++; # $a is now 91
--$a; # $a is now 90
$result = ($a + 2) * 3.4; # $result is 312.8
22/09/2012 Mirafra Confidential 12
Numerical Scalar Variables (Contd …)

use strict;
$this_data = "Something";
$that_data = "Something Else ";
print "_$this_data_, or $that_datawill do\n";

# INVALID: actually refers to the scalars


$this_data_ and $that_datawill

print "_${this_data}_, or ${that_data}will do\n";

use strict forces you to declare your variables before using them. Also
controls references & subroutines.
22/09/2012 Mirafra Confidential 13
User Input

 <STDIN> grabs one line of input, including the newline character. So,
after:
$name = <STDIN>;
if the user typed “Bill Clinton[ENTER]”,
$name will contain: “Bill Clinton\n ”.

 To delete the newline, the chomp() function takes a scalar variable,


and removes the trailing newline if present. (If there is no newline at
the end, it does nothing)
chomp($name);

 A shortcut to do both operations in one line is:


chomp($name = <STDIN>);

22/09/2012 Mirafra Confidential 14


Input from STDIN

 $cat file
print "What is your name? ";
$name = <STDIN>;
chomp ($name);
print “Hello $name !\n”;

Output:
$ ./file
What is your name? Arpit
Hello Arpit !
$

22/09/2012 Mirafra Confidential 15


Chop and Chomp

 chop : A built-in function is chop. Takes a single argument


and removes the last character.
$x = "hello world";
$y=chop($x); # $x is now "hello worl" , $y is d
$k=chomp($x); # $x is “hello worl” , $k is 0

$x=“hello world\n”;
$y=chop($x); # $x is now “hello world”, $y is “\n”

$x = "hello world\n";
$k=chomp($x); # $x is “hello world”, $k is 1
22/09/2012 Mirafra Confidential 16
Strings (Singly quoted) (1)

 Single quoted strings: Prints string as it is !


 Except ‘ and \

 Examples:

- 'I don\'t think so.' # I don’t think so.


- 'Need a \\ or \?'; # Need a \ or \?
- 'You can do this: \\'; # You can do this: \
- 'Three \\\'s: "\\\\\\"'; # Three \’s: "\\\”
- ‘Time to start # Time to start
in new line ‘ in new line

22/09/2012 Mirafra Confidential 17


Strings (Singly quoted) (2)

 Invalid Strings:

- 'You cannot do this: \';


- 'It is 5 o'clock!'
- 'Three \'s: \\\\\';
- 'This is my string;

22/09/2012 Mirafra Confidential 18


Strings (Doubly quoted) (1)

“\\” - an actual, single backslash character


“\$” - a single $ character
”\@” - a single @ character
“\n” - newline
“\e” - escape
“\t” - tab
“\b” - backspace
“\101” - character represented by octal value, 101 => A
“\x41” - character represented by hexa-decimal value, 41 => A

 A feature of double-quoted strings is that they are variable interpolated,


meaning that scalar and array variables within the strings are replaced with
their current values when the strings are used.

22/09/2012 Mirafra Confidential 19


Back quotes: Command Substitution

 You can use command substitution in Perl like in shell


scripts:
#!/usr/local/bin/perl5 -w
$user = `whoami`;
chomp($user);
$num = `who | wc -l`;
chomp($num);
print "Hi $user! There are $num users logged on.\n";
 Command substitution will usually include a newline, so
use chomp().
22/09/2012 Mirafra Confidential 20
String Operators

 String values can be concatenated with the "." operator.


"hello" . "world\n" # same as "helloworld\n"
"fred" . " " . "barney" # same as "fred barney”

String repetition operator : x


"fred" x 3 # is fredfredfred
“barney" x (4+1) # is barneybarneybarneybarneybarney
(3+2) x 4 # is 5555

Comparison operators for strings:


eq , ne , lt , gt , le , ge

22/09/2012 Mirafra Confidential 21


Scalar interpolation in strings

$a = "fred";
$b = “Hi $a”; # $b is now Hi fred

$a= ‘$fred’; # $a is now $fred


$b= “Hey $a”; # $b is now Hey $fred

22/09/2012 Mirafra Confidential 22


REF
Scalar interpolation in strings

$fred = "pay";
$fredday = "wrong!";

$barney = "It's $fredday"; # It’s wrong!


$barney = "It's ${fred}day"; # It’s payday
$barney2 = "It's $fred"."day"; # It’s payday
$barney3 = "It's " . $fred . "day"; # It’s payday

$str = “hello”
$str .= “add"; # $str is now helloadd
22/09/2012 Mirafra Confidential 23
REF
Assignment with operators

use strict;
my $greet = "Hi! ";

$greet .= "Everyone\n";
$greet = $greet . "Everyone\n“; # Does the same operation as the line above

print $greet; # Hi! Everyone


Everyone

22/09/2012 Mirafra Confidential 24


String functions (1)

 Substring extraction:
substr( STRING, OFFSET[, LEN] [, REPLACEMENT])

$a = “123456789” ;
print substr($a,3); #prints 456789
print substr($a,3,2) #prints 45
substr($a,3,2) = ”World” ;
print $a ; # prints 123World6789
substr($a,3) = ”Ending” ;
print $a ; # prints 123Ending
substr($a,3,2,“World”);
print $a; # 123Worldding

22/09/2012 Mirafra Confidential 25


String functions (2)

 String length:
length STRING

Ex:
$str=“abc”;
$a=length $str;
print $a; # $a is 3

22/09/2012 Mirafra Confidential 26


Numeric Operators

 Perl provides the typical ordinary opertions:


For example:
2+3 #Output: 5
5.1 – 2.4 #Output: 2.7
3 * 12 #Output: 36
14 / 2 #Output: 7
10 % 3 #Output: 1
2**3 #Output: 8
Comparison : < <= == >= > !=

22/09/2012 Mirafra Confidential 27


REF

Numerical Example

#!/usr/local/bin/perl5 -w
print "Enter height of rectangle: ";
$height = <STDIN>;
print "Enter width of rectangle: ";
$width = <STDIN>;
$area = $height * $width;
print "The area of the rectangle is $area\n";
Output:
Enter height of rectangle: 10.1
Enter width of rectangle: 5.1
The area of the rectangle is 51.51

22/09/2012 Mirafra Confidential 28


REF

Conversions : Numbers & Strings

 If a string value is used as an operand for a numeric


operator (say, +), Perl automatically converts the
string to its equivalent numeric value, as if it had
been entered as a decimal floating point value

3 + “123.45fred” # 3 + 123.45 = 126.45


“2.5a” + ”2b” # 2.5 + 2 = 4.5
“abc2” + “2” #0+2=2
"X" . (4 * 5) # same as "X" . 20, or "X20"
"X" x (2 * 2) # same as "X" x 4, or "XXXX"
22/09/2012 Mirafra Confidential 29
REF

Scalar Operators & Functions

$a = 17;
$b = $a + 3; # $b is 20 now
$b = 4 + ($a = 3); # $b is 7 now
$d = ($c = 5); # copy 5 to $c and then 5 to $d
$a += 5; # $a is 8 now
$b *= 3; # same as $b=$b*3

22/09/2012 Mirafra Confidential 30


REF

Auto- Increment with Operators

use strict;
my $abc = 5;

my $efg = $abc-- + 5;
# $abc is now 4, but $efg is 10

my $hij = ++$efg - --$abc;


# $efg is 11, $abc is 3, $hij is 8

22/09/2012 Mirafra Confidential 31


Comparison Numeric String
----------------------------------------------------------------
equal = eq
not equal != ne
less than < lt
more than > gt
less than equal to <= le
greater than equal to >= ge

22/09/2012 Mirafra Confidential 32


Undef

 Perl never complains…


 Variables have the undef value before they are first
assigned.
 This value looks like a zero when used as a number, or
the zero-length empty string when used as a string

$a=$b+10;
#use strict wont let you do this !
print $a; #prints 10

22/09/2012 Mirafra Confidential 33


Control Flow

 Perl has several control flow statements:


 if
 while
 for
 unless
 until
 do while
 do until
 foreach

22/09/2012 Mirafra Confidential 34


“ if “ statement

 The Perl if statement works almost the same as in C++:


$user = `whoami`;
chomp($user);
if($user eq "clinton")
{
print "Hi Bill!\n";
}
 The eq operator compares two strings, and returns true if they are
equal (use == for numeric comparisons).

 The curly braces { } are always required in Perl even if there is only
one statement inside.

22/09/2012 Mirafra Confidential 35


“ if … else “ statement

 The if else statement is similar:

$user = `whoami`;
chomp($user);
if ($user eq "clinton") {
print "Hi Bill!\n";
} else {
print "Hi $user!\n";
}

22/09/2012 Mirafra Confidential 36


“ if … elsif … else “ statement

 You can also handle a list of cases:


$users = `who | wc -l`;
chomp($users);
if ($users > 4){
print "Heavy load!\n";
}
elsif ($users > 1){ # Notice no ‘e’ in elsif
print "Medium load\n";
}
else {
print "Just me!\n";
}

22/09/2012 Mirafra Confidential 37


Unless - condition

print "how old are you? ";


$a = <STDIN>;
chomp($a);
unless ($a < 18)
{
print "Old enough! Cool! So go vote!\n";
}

22/09/2012 Mirafra Confidential 38


Truth in Perl

 Truth is flexible in Perl:


 Expressions that evaluate to false
0 # traditional false value
"" # the null string
"0" # only non-zero length false string
 Some examples of truth:
1 # traditional true value
684 # non-zero numerical values are true
"" # whitespace is true
"hello" # strings are true
"00" # a string

22/09/2012 Mirafra Confidential 39


Ex:

0 # converts to "0", so false


1-1 # computes to 0, so false
1 # converts to "1", so true
"" # empty string, so false
"1" # not "" or "0", so true
"00" # not "" or "0", so true
"0.000" # also true for the same reason
undef # evaluates to "" or 0, so false

22/09/2012 Mirafra Confidential 40


REF

And, Or, Not

 You can also combine and negate expressions with logical and (&&), logical
or (||), and not (!) just like in C++:

chomp($user = `whoami`);
chomp($nme = `who | grep $user | wc -l`);
chomp($nusers = `who | wc -l`);
if($nusers - $nme && $user ne "clinton"){
print "Someone else is logged in!\n";
}
else{
print "All is well!\n";
}

22/09/2012 Mirafra Confidential 41


“ while “ statement

 The while statement loops indefinitely, while the condition is true, such as a
user-controlled condition:

$resp = "no";
while($resp ne "yes"){
print "Wakeup [yes/no]? ";
chomp($resp = <STDIN>);
}

22/09/2012 Mirafra Confidential 42


Looping using for

 for can be used as in C++ to do incrementing loops:


$ cat fac

print "Enter number: ";


$n = <STDIN>;
$fac = 1;
for($i=1; $i<=$n; $i++){
$fac *= $i;
}
print "The factorial of $n is $fac\n";
$ ./fac
Enter number: 5
The factorial of 5
is 120 Why the output is printed in two lines???
$
22/09/2012 Mirafra Confidential 43
“last “ command

 The last command works like the C++ break command,


breaking out of the innermost loop :

while(1){
print "Wakeup [yes/no]? ";
chomp($resp = <STDIN>);
if($resp eq "yes"){
last;
}
}

22/09/2012 Mirafra Confidential 44


Until - condition

until (some_expression){
statement_1; statement_2; statement_3;
}

Ex:
$a = 1; # Count from 1 to 10
until ($a > 10) {
print "a = $a\n";
$a++;
}

22/09/2012 Mirafra Confidential 45


“do .. while “ statement

$count = 10;
do {
print "$count ";
$count--;
} while ($count >= 1);

Output: 10 9 8 7 6 5 4 3 2 1

22/09/2012 Mirafra Confidential 46


“do .. until “ statement

$count = 10;
do {
print "$count ";
$count--;
} until ($count == 0);

Output: 10 9 8 7 6 5 4 3 2 1

22/09/2012 Mirafra Confidential 47


Operator Precedence

 Operator precedence is basically the same as in C++.


 As in C++, you can use parentheses to override
precedence, and to clarify the grouping.
Operator Associativity
------------ ------------------------
** right 2** 6 **2= 2**36
*, /, % left
+, - left

22/09/2012 Mirafra Confidential 48


Operator Precedence

< 1 $left is less than $right


<= 1 $left is less than or equal to $right
> 1 $left is greater than $right
>= 1 $left is greater than or equal to $right
== 1 $left is the same as $right
!= 1 $left is not the same as $right
<=> -1 $left is less than $right, (cmp for string comparison)
0 $left is equal to $right
1 $left is greater than $right

22/09/2012 Mirafra Confidential 49


Examples

use strict;
my $a = 5; my $b = 500;
$a < $b; # evaluates to 1 true
$a >= $b; # evaluates to "“ false
$a <=> $b; # evaluates to -1 true

my $c = "hello"; my $d = "there";
$d cmp $c; # evaluates to 1 true
$d ge $c; # evaluates to 1 true
$c cmp "hello"; # evaluates to "" false

22/09/2012 Mirafra Confidential 50


Perl Arrays and Lists

 To understand the format and the declaration of Arrays in Perl


 To study the basic operations of Arrays in Perl

22/09/2012 Mirafra Confidential 51


Lists

 A list is an ordered collection of scalar data.


 A list begins and ends with parentheses, with the elements separated by
commas (and optional spaces).
(1,2, 3,4.1)

 List elements can be constants or expressions:


("Bill", 4, "pie", "B. Gates")
($num, 17, $num+1+$i)

 List variables & List accessing:


@a=(1,2,3);
print @a; # prints 123
print (1,2,3); # prints 123
22/09/2012 Mirafra Confidential 52
Lists

 The empty list (no elements) is represented by an empty pair of parenthesis:


() # empty list

 The list constructor “..” creates a list of values with increments of 1:


(1 .. 4) # same as (1, 2, 3, 4)
(1..4) # same as (1, 2, 3, 4)
(1, 5 .. 7) # same as (1, 5, 6, 7)
($min .. $max) # depends on values of $min and $max
(10 .. 5) # same as ( ) -> it can’t count down

22/09/2012 Mirafra Confidential 53


Single- Word Lists

 There is a shortcut for lists of single-word strings, the “quote word” function:

("bill", "gates", "pie", "toss") # usual version

qw(bill gates pie toss) # same as above

qw(bill
gates
pie
toss) # same as above

22/09/2012 Mirafra Confidential 54


Arrays

 An array contains a list (zero or more scalar values).


 Array variable names are similar to scalar variable names, except the initial
character is “@” instead of “$”.
@numbers = (1,2, 3,4.1);
@all = @numbers; # copies array to @all
@list1 = ("Bill", 4, "pie", "B. Gates");
$num = 2;
@group = ($num, 17, $num+1);

 If a scalar value is assigned to an array variable, it becomes a single-


element list automatically:
@a = 4; # becomes (4) automatically

22/09/2012 Mirafra Confidential 55


Inserting Arrays

 You can also insert array elements into lists:


@numbers = (6,7,8);
@numbers = (1, 2, @numbers, 10); # (1,2,6,7,8,10)
@numbers = (0, @numbers); # (0,1,2,6,7,8,10)
@numbers = (@numbers, 99); # (0,1,2,6,7,8,10,99)
 Note that the inserted array elements are at the same
level as the other elements, not in a “sublist”.

22/09/2012 Mirafra Confidential 56


Left- Side Assignment

 If a list only contains variables, you can use it on the left


side of an assignment:
($a,$b,$c) = (1,2,3); # set $a=1, $b=2, $c=3
($a,$b) = ($b,$a); # swap $a and $b so, $a=2, $b=1
($d,@bill) = ($a,$b,$c); # set $d=$a=2 and @bill=($b,$c)=(1,3)
($e,@bill) = @bill; # remove first element of @bill
# and put it in $e
# end up with: $a=2, $b=1, $c=3
# $d=2, $e=1, @bill=(3)
(@bill, $e) = @bill; # no change to @bill

 An array variable can only occur in the last position in the


list, because the array variable is “greedy” and consumes
all the remaining values.
22/09/2012 Mirafra Confidential 57
Array Length

 If an array variable is assigned to a scalar variable, the number assigned is


the length of the array:
@nums = (1,2,3);
$n = @nums; # $n gets 3, the length of @nums

 The context determines whether the length of the array is used or the list:
$n = @nums; # $n gets the length of @nums
($n) = @nums; # $n gets the first element of @nums
($n1, $n2) = @nums; # first two elements of @nums

 The first assignment is a scalar assignment, so @nums is treated as a scalar,


returning its length.
 The second assignment is an array assignment, and gives the first element of
@nums (silently discarding the rest).

22/09/2012 Mirafra Confidential 58


Array Subscripting

 Each element of an array can be accessed by its integer


position in the list.
 The first element starts at position 0 (like C++ arrays).
 The first element of the @a array is accessed as $a[0]:
@a = qw(bill gates pie toss);
$name1 = $a[0]; # sets name1 to "bill"
$name2 = $a[3]; # sets name2 to "toss"
$a[1] = "clinton"; # a: qw(bill clinton pie toss)
 Note that the @ on the array name becomes a $ when
accessing individual elements.

22/09/2012 Mirafra Confidential 59


Array Subscripting

 You can use all the usual scalar operations on the array
elements:
@a = (1,2,3);
$a[0]++; # a: (2,2,3)
$a[1] += 4; # a: (2,6,3)
$a[2] += $a[0]; # a: (2,6,5)

# swap the first two elements


($a[0],$a[1]) = ($a[1],$a[0]); # a: (6,2,5)

22/09/2012 Mirafra Confidential 60


Array Slices

 Accessing a list of elements from the same array is called


a slice.
 Perl provides a special shortcut for slices:
@a = (1,2,3);
@a[0,1] = @a[1,0]; # swap the first two elements
@a[0,1,2] = @a[1,1,1]; # make all 3 elements to 2nd element
@a[1,2] = (7,4); # change the last two to 7 and 4
# a: (1,7,4)
 Note that slices use @ rather than $. This is because
slices work with lists rather than scalar values.

22/09/2012 Mirafra Confidential 61


List Slices

 Slices also work directly on lists:

$c = (1,2,3,4,5)[2]; # sets $c to 3
@b = (1,2,3,4,5)[2,4]; # sets @b to (3,5)

 The second statement above is equivalent to:

@x = (1,2,3,4,5);
@b = @x[2,4];

22/09/2012 Mirafra Confidential 62


Index Expressions

 You can use expressions for the subscripts just like in


C++:

@list1 = (5,6,7);
$n = 2;
$m = $list1[$n]; # $m = 7
$p = $list1[$n-1]; # $p = 6
($p) = (5,6,7)[$n-1]; # same thing using slice

22/09/2012 Mirafra Confidential 63


Slice Expressions

 You can also use array expressions to index slices if you


want to be tricky:

@nums = (5,6,7);
@i = (2,1,0);
@rev = @nums[@i];
# same as @nums[2,1,0]
# or ($nums[2], $nums[1], $nums[0])
# or (7,6,5)

22/09/2012 Mirafra Confidential 64


Demo – 1 (Arrays)

 Write a script to find the largest value in an array.


 Write a script to find the sum of elements in an array.

22/09/2012 Mirafra Confidential 65


Labs – 1 (Arrays)

 Write a script to find the Nth largest value in an array.


 Write a script to find and remove duplicate elements from
an array.
 Write a script to reverse a string.
 Write a script to find out all prime numbers up to a certain
number (user input).

22/09/2012 Mirafra Confidential 66


Bad Subscripting

 If you access an array beyond the end of the array, the


undef value is returned without warning.
 undef is 0 when used as a number, the empty string
when used as a string.

@nums = (5,6,7);
$nums[3] = "bill"; # @nums: (5,6,7,"bill")
$nums[5] = "g."; # @nums: (5,6,7,"bill",undef,"g.")

22/09/2012 Mirafra Confidential 67


Backward Subscripting

 You can use $#bill to get the index value of the last element of @bill.
 Accessing an array with a negative subscript counts back from the
end. So, another way to get the last element of @bill is $bill[-1].

@bill = qw(rich rich lucky likespie);


print $#bill; # prints 3
print $bill[$#bill]; # prints likespie
print $bill[$#bill-1]; # prints lucky
print $bill[-1]; # prints likespie
print $bill[-2]; # prints lucky
print $bill[-3]; # prints rich

22/09/2012 Mirafra Confidential 68


“ x ” operator for lists

 The x constructor also replicates a list:

@ones = (1) x 80;


# a list of 80 1s
@ones = (5) x @ones;
# set all elements to 5

22/09/2012 Mirafra Confidential 69


“push” and “pop”

 You can use push and pop to add and remove values
from the end of an array.
@a = (1,2,3);
$new = 6;
push(@a,$new); # same as @a = (@a,$new)
# so far: (1,2,3,6)
$oldvalue = pop(@a); # removes last element of @a
# so far: (1,2,3)
push(@a,4,5,6); # can push multiple values
# so far: (1,2,3,4,5,6)
 pop returns undef if given an empty array.

22/09/2012 Mirafra Confidential 70


“unshift” and “shift” an Array

 You can use unshift and shift to add and remove values from the
beginning of an array.
@a = (1,2,3);
$new = 6;
unshift(@a,$new); # same as @a = ($new, @a)
# so far: (6,1,2,3)
$old = shift(@a); # removes first element of @a
# so far: (1,2,3)
unshift(@a,4,5,6); # can unshift multiple values
# same as @a = (4,5,6,@a)
# so far: (4,5,6,1,2,3)
 shift returns undef if given an empty array.

22/09/2012 Mirafra Confidential 71


Stack

use strict;
my @stack;

push(@stack, 7, 6, "go");
# @stack is now qw/7 6 go/
my $action = pop @stack;
# $action is "go", @stack is (7, 6)
my $value = pop(@stack) + pop(@stack);
# value is 6 + 7 = 13, @stack is empty

22/09/2012 Mirafra Confidential 72


Queue

use strict;
my @queue;
unshift (@queue, "Customer 1");
# @queue is now ("Customer 1")
unshift (@queue, "Customer 2");
# @queue is now ("Customer 2" "Customer 1")
unshift (@queue, "Customer 3");
# @queue is now ("Customer 3" "Customer 2" "Customer 1")
my $item = pop(@queue);
# @queue is now ("Customer 3" "Customer 2")

22/09/2012 Mirafra Confidential 73


“reverse” and “sort” an Array

 The reverse function reverses the array, returning the resulting list:
@a = (1,2,3);
@b = reverse(@a); # @b=(3,2,1), @a unchanged
@b = reverse(1,2,3); # same thing
@b = reverse(@b); # @b=(1,2,3)

 The sort function returns a sorted array in ascending ASCII order:


@size = qw(small medium large);
@sortsize = sort(@x); # large, medium, small
@sortsize = sort(qw(small medium large)); # same
@a = (1,2,4,8,16,32,64);
@b = sort(@a); # @b=(1,16,2,32,4,64,8)

22/09/2012 Mirafra Confidential 74


Array Variable Interpolation

 Array elements can be interpolated in double-quoted strings:

@comp111 = qw(unix shell perl);


print "$comp111[0] programming is mainly done ";
# unix programming is mainly done

 Arrays and array slices will be interpolated (printed) with spaces between the
elements:

@a = (1,2,"bill",3);
print @a; # prints 12bill3
print "a: @a\n"; # prints a: 1 2 bill 3
print "a1: @a[1,2]\n"; # prints a1: 2 bill

22/09/2012 Mirafra Confidential 75


@ and $ Review

 @ (at sign)
 Refers to the entire array or
 slice of an array (when used with [ ]).

 $ (dollar sign)
 Refers to one element of the array, used with []

22/09/2012 Mirafra Confidential 76


Array chomp

 The chomp function also works for array variables, removing any ending
newlines in each element :
@comp111 = (“unix\n”, “shell\n”, “perl\n”);
chomp(@comp111);
# @comp111 is now (“unix”, “shell”, “perl”)

 Array chomp is especially useful when reading a file or input from a user.

 <STDIN> in a list context will return all remaining lines up to the end of the
file, or until the user hits CTRL-D.
@lines = <STDIN>; # read input in a list context
chomp(@lines); # remove all trailing newlines

22/09/2012 Mirafra Confidential 77


“ foreach “ statement (1)

 foreach takes a list of values and assigns them one by one to a


scalar variable.
 The body of the loops is executed once for each successive
assignment.
 foreach is similar to the shell programming’s for statement.

foreach $i (@some_list){
...
}

22/09/2012 Mirafra Confidential 78


“ foreach “ statement (2)

 The following example prints the numbers in reverse order without changing
the array:

#!/usr/local/bin/perl5 -w
@a = (1,2,3,4,5);
foreach $i (reverse @a){
print "$i ";
}
print "\n"; # 5 4 3 2 1

 reverse @a is the same as writing reverse(@a).


 Parentheses are always optional on Perl functions.

22/09/2012 Mirafra Confidential 79


“ foreach “ statement (3)

 If you omit the scalar variable in foreach, Perl will use $_ automatically:

$ cat print3
#!/usr/local/bin/perl5 -w
@a = (1,2,3,4,5);
foreach (reverse @a){
print;
}
print "\n"; # 54321

 print (and other Perl functions) use $_ as the default variable, if nothing is
specified.

22/09/2012 Mirafra Confidential 80


“ foreach “ statement (4)

 The scalar variable in foreach is an alias for each variable in the list,
not a copy. (Tricky!)
 If you modify the scalar variable in foreach, the aliased element in
the list is also changed:

#!/usr/local/bin/perl5 -w
@a = (1,2,3,4);
foreach $b (@a){
$b *= 2;
}
print "@a\n"; #2468

22/09/2012 Mirafra Confidential 81


Splice

 splice ARRAY[,OFFSET][,LENGTH][,LIST]
 Similar to substr( STRING, OFFSET[, LEN] [, REPLACEMENT]) for strings.

 Removes the elements designated by OFFSET and LENGTH from an array, and
replaces them with the elements of LIST

@browser = ("NS", "IE", "Opera“, “IE7”);


splice(@browser, 1, 2, "NeoPlanet“,"Mosaic“,”Firefox”);
print “@browser\n” # NS NeoPlanet Mosaic Firefox IE7

22/09/2012 Mirafra Confidential 82


Splice (contd …)

 splice ARRAY,OFFSET,LENGTH
 Removes the elements designated by OFFSET and LENGTH
from an array

 splice ARRAY,OFFSET
 Removes all the elements from OFFSET onwards

 splice ARRAY
 Removes everything

22/09/2012 Mirafra Confidential 83


REF
Splice (contd …)

 Following Statements are equivalent

 push(@a,$x,$y) splice(@a,@a,0,$x,$y)
 pop(@a) splice(@a,-1)
 shift(@a) splice(@a,0,1)
 unshift(@a,$x,$y) splice(@a,0,0,$x,$y)
 $a[$i] = $y splice(@a,$i,1,$y)

22/09/2012 Mirafra Confidential 84


REF
Splice (contd …)

 In scalar context, splice returns the last element


removed, or undef if no elements are removed. The array
grows or shrinks as necessary.

 If OFFSET is negative then it starts that far from the end


of the array.

 If LENGTH is negative, removes the elements from


OFFSET onward except for -LENGTH elements at the
end of the array.

22/09/2012 Mirafra Confidential 85


Perl I/O

22/09/2012 Mirafra Confidential 86


Input from STDIN (1)

 Reading from STDIN is easy, and we have done it many times.

$a = <STDIN>;

@a = <STDIN>;

 Each element is one line, and includes the terminating newline (the last line
may or may not have a newline).

22/09/2012 Mirafra Confidential 87


Input from STDIN (2)

 Typically, a program will read in one line at a time and process the line:

while($line = <STDIN>){
# process line
print "$line";
}

$ ./line1
Hi hello
Hi hello
test
test
[CTRL-d] $

22/09/2012 Mirafra Confidential 88


Input from STDIN (3)

 We can also read and print the lines altogether:

@lines = <STDIN>;
# process lines
print "@lines";

$ ./line3
hi
test
[CTRL-d] hi
test
$

22/09/2012 Mirafra Confidential 89


Input from STDIN (4)

 Perl has a shortcut for reading a value from <STDIN> into $_.
 Whenever a condition consists solely of <STDIN>, Perl automatically copies
the line into $_.

while(<STDIN>){ # while($_ = <STDIN>){


chomp; # chomp "$_";
print; # print "$_";
}

22/09/2012 Mirafra Confidential 90


Input from <> (1)

 Another way to read input is with <>.


 However, unlike <STDIN>, <> gets its data from the file (or files) specified on
the command line.

while(<>){ $cat f1
print; 1
} $cat f2
./prg f1 f2 2
1
2

22/09/2012 Mirafra Confidential 91


Input from <> (2)

 If you do not specify any filenames on the command line, <> reads from
standard input automatically.

while(<>) {
print;
}

$ perl 1.pl
Hi
hi
test
test
[CTRL-d] $

22/09/2012 Mirafra Confidential 92


REF
Output to STDOUT

 The print function takes a list of strings, and sends each to STDOUT,
without adding any characters between them.

print "Hi Bill!\n"; #Hi Bill!


print "Hi “ . "Bill!“. "\n"; #Hi Bill! (Here .(dot) or ,(comma) both works for print)

 Sometimes, you will need to add parentheses to print, especially when the
first argument starts with a left parentheses:

print (1+1), “ rich Bill\n"; #2


print ((1+1), “ rich Bill\n"); #2 rich Bill
print 1+1, “ rich Bill\n"; #2 rich Bill
print “hi ”,(1+1),“ rich Bill\n” #hi 2 rich Bill

22/09/2012 Mirafra Confidential 93


printf Formatted Output (1)

 You may wish to have more control over your output than print provides.
 The printf function takes a list of arguments, where the first argument is a
format control string, which defines how to print the remaining arguments
(like printf in C).

printf "%s %d %f\n", $s, $n, $real;

Format types:
strings %s
integer numbers %d
floating-point numbers %f

22/09/2012 Mirafra Confidential 94


printf Formatted Output (2)

$s = "1234567890";
$n = 12345;
$real = 1234567.123;
printf “[%15s] [%5d] [%10.2f]\n", $s, $n, $real;
# [ 1234567890] [12345] [1234567.12]
 This example prints $s in a 15-character field, then space, then $n as
a decimal integer in a 5-character field, then another space, then
$real as a floating-point value with 2 decimal places in a 10-
character field, and finally a newline.

printf “[%5s] [%1d] [%5.5f]\n", $s, $n, $real;


# [1234567890] [12345] [1234567.12300]

22/09/2012 Mirafra Confidential 95


Perl Hashes
 Learning Objectives:
 To understand the concept of Hash as a data
structure
 To learn the operations of Hash which are
available in Perl
 To understand the usage & application of hash in
Perl

22/09/2012 Mirafra Confidential 96


What is a Hash?

 A hash (or associative array) is like an array, where the index can be
any scalar value (not just small non-negative integers).
 A hash index is called a key (keys must be unique).
 The elements of a hash have no fixed order, unlike arrays.
 The keys are used to lookup the values.

KEYS “bill” 16 “0 C”

VALUES “rich” 2046.1 “32 F”

22/09/2012 Mirafra Confidential 97


Declaring Hashes

 To initialize hashes:

$ cat morehash
#!/usr/local/bin/perl5 -w
%bill = ( "Gates" => “rich", "Clinton" => "busy");
print "Hello $bill{'Gates'} Bill!\n";

$ ./morehash
Hello rich Bill!

 You may use single quotes for specifying a key and values

22/09/2012 Mirafra Confidential 98


Hash Variables (1)

 Hash variable names begin with the percent sign (%) followed by the
usual variable name.

 There is no relationship between $bill, @bill, and %bill, Perl


considers them to be separate variables.

 Each element of a hash is a separate scalar variable, accessed by


the key.

 Elements are accessed with a subscript in curly braces { } as


$var_name{$key} where $key is any scalar expression. Ex:
$bill{“Gates”}

22/09/2012 Mirafra Confidential 99


Hash Variables (2)

 As with arrays, you create new hash elements:


$bill{"Gates"} = “rich";
$bill{"Clinton"} = "busy";
$bill{234.5} = 456.7;
 Once created, you can access hash values similar to indexing arrays:
print "Bill Gates is ", $bill{"Gates"}, "\n";
$n = "Clinton";
print "Bill $n is $bill{$n}\n";
$n = 234.5;
print "Bill $n is $bill{$n}\n";
 Output:
Bill Gates is rich
Bill Clinton is busy
Bill 234.5 is 456.7

22/09/2012 Mirafra Confidential 100


Hash Variables (3)

 Once created, you can change hash values if needed:

$bill{234.5} = 456.7;
...
$bill{234.5} += 3; # makes it 459.7

 Referencing a hash element that does not exist returns the undef value.

22/09/2012 Mirafra Confidential 101


REF
List Representation of a Hash

 You can access the hash as a whole if you need to initialize it or to copy it.
 The hash unwinds as a list. Each pair in the list represents the key and its
value.

$bill{"Gates"} = "rich";
$bill{"Clinton"} = "busy";
$bill{234.5} = 456.7;
@billarray = %bill;
# @billarray: qw(Gates rich 234.5 456.7 Clinton busy)
%a = @billarray; # create %a like %bill
%a = %bill;
%b = qw(Gates rich Clinton busy 234.5 456.7);
# initialize %b like %bill from list values

22/09/2012 Mirafra Confidential 102


REF
Hash “reverse”

 You can construct a hash with keys and values swapped using the reverse
function:
%aback = reverse %a;

 If %a has two identical values, those will end up as a single element in


%aback (reverse is best used on hashes with unique keys and values).
#!/usr/local/bin/perl5 -w
$b{"Gates"} = "Bill";
$b{"Clinton"} = "Bill";
print %b; #GatesBillClintonBill
%revb = reverse %b;
print %revb #BillGates

22/09/2012 Mirafra Confidential 103


Retrieving “keys” in a Hash

 The keys(%hashname) function returns a list of all the keys currently in the
hash.
%bill = ( "Gates" => "rich", "Clinton" => "busy");

 As with other Perl functions, the parentheses are optional:


@list = keys %bill; #(Gates,Clinton)

 In a scalar context, keys returns the number of elements in the hash:


$no_keys = keys %bill; #2

22/09/2012 Mirafra Confidential 104


Printing Hashes

 The keys function is often used in foreach loops to print or access the
elements one-by-one:

#!/usr/local/bin/perl5 -w
$bill{"Gates"} = “rich";
$bill{"Clinton"} = "busy";
$bill{234.5} = 456.7;
foreach $k (sort(keys(%bill))){ #Here sort sorts keys in ASCII ascending order
print "At $k we have $bill{$k}\n"; # Just like linux/shell sort command, which sorts
} # contents of a file in ASCII ascending order

At 234.5 we have 456.7


At Clinton we have busy
At Gates we have rich
22/09/2012 Mirafra Confidential 105
Retrieving “values” in a hash

 The values(%hashname) function returns a list of all the values currently in


the hash.
 The values are in exactly the same order as the keys returned by
keys(%hashname).
$bill{"Gates"} = “rich";
$bill{"Clinton"} = "busy";
$bill{234.5} = 456.7;
@klist = keys(%bill);
@vlist = values(%bill);
print "@klist\n";
print "@vlist\n";
234.5 Gates Clinton  keys
456.7 rich busy  values

22/09/2012 Mirafra Confidential 106


Printing Hashes

 You cannot print the entire hash like you can print arrays:

#!/usr/local/bin/perl5 -w
$bill{"Gates"} = "Bill";
$bill{"Clinton"} = "Bill";
$bill{234.5} = 456.7;
print "Bill hash: %bill\n"; #Bill hash: %bill
print %bill; #GatesBillClintonBill234.5456.7

22/09/2012 Mirafra Confidential 107


each

 Another way to print a hash is with the each function.


 each returns a key-value pair as a two-element list.
 Each time each is called, it returns the next key-value pair until all the
elements have been accessed.
 When no more pairs, each returns an empty list.
$bill{"Gates"} = “rich";
$bill{"Clinton"} = "busy";
$bill{234.5} = 456.7;
while(($k,$v) = each(%bill)){
print "At $k we have $v\n";
}
At 234.5 we have 456.7
At Gates we have rich
At Clinton we have busy

22/09/2012 Mirafra Confidential 108


“delete” a Hash element with a key

 To remove hash elements use the delete function.


 The argument is the keyed reference to be deleted:

$bill{"Gates"} = “rich";
$bill{"Clinton"} = "busy";
$bill{234.5} = 456.7;
delete $bill{"Gates"};
while(($k,$v) = each(%bill)){
print "At $k we have $v\n";
}

At 234.5 we have 456.7


At Clinton we have busy

22/09/2012 Mirafra Confidential 109


Hash Slices (1)

 Like an array, a hash can be sliced to access a collection of elements.


 We can use a hash slice to compact initialization.
For example:
$b{"Gates"} = “rich";
$b{"Clinton"} = "busy";
$b{234} = 45;
can be shortened to:
($b{"Gates"},$b{"Clinton"},$b{234}) = qw(rich busy 45);
can be hash sliced as:
@b{"Gates","Clinton",234} = qw(rich busy 45);
(Note that it is @b, not %b)
 Another example:
@b{"A" .. "Z"} = (1 .. 26);

22/09/2012 Mirafra Confidential 110


Hash Slices (2)

 We can also use a hash slice with variable interpolation :

#!/usr/local/bin/perl5 -w
@b{"Gates","Clinton",234} = qw(rich busy 45);
@k = qw(Gates Clinton);
print "The values are: @b{@k}\n";
# The values are: rich busy

 If n+k values assigned to a hash of n keys then it eliminates the remaining k


values.

22/09/2012 Mirafra Confidential 111


Hash Slices - Merging Hashes (1)

 Hash slices can also be used to merge hashes.


 In the following example, if there are duplicate keys, the values overwritten
by the %small hash:
@big{"Gates","Clinton",234} = qw(rich busy 45);
@small{"Horner",234} = qw(111 67);
@big{keys %small} = values %small;
while(($k,$v) = each(%big)){
print "At $k we have $v\n";
}

At 234 we have 67
At Gates we have rich
At Horner we have 111
At Clinton we have busy

22/09/2012 Mirafra Confidential 112


Hash Slices - Merging Hashes (2)

 A simpler (though slower) way to merge hashes is:


$ cat merge1
#!/usr/local/bin/perl5 -w
@big{"Gates","Clinton",234} = qw(rich busy 45);
@small{"Horner",234} = qw(111 67);
%big = (%big, %small);
while(($k,$v) = each(%big)){
print "At $k we have $v\n";
}
$ ./merge1
At 234 we have 67
At Gates we have rich
At Horner we have 111
At Clinton we have busy

22/09/2012 Mirafra Confidential 113


Demo – 2 (Hashes)

 Extract unique elements from an array.


 Find union, intersection and symmetric difference of two arrays
(Assumption: Elements in each array are unique)

22/09/2012 Mirafra Confidential 114


Labs – 2 (Hashes)

 Write a script to find the largest value in an hash.


 Write a script to insert new values into hash.
 Write a script to change keys/values inside a hash.
 Write a script to extract the duplicate values appearing in a hash.

22/09/2012 Mirafra Confidential 115


Command Line & Environment (1)

 The name of your Perl script is $0


 The array for command line parameter is @ARGV
 The process ID of your Perl script is $$
 The hash containing your current environment: %ENV

22/09/2012 Mirafra Confidential 116


Command Line & Environment (2)

$ cat p1
#!/usr/local/bin/perl5 -w
$argc = @ARGV;
print "parameter count $argc for $0 (PID: $$)\n";
print "parmamters: $ARGV[0] $ARGV[1]\n";
print "You are $ENV{USER} \n";

$ ./p1 a b
parameter count 2 for p1 (PID: 20489)
parmamters: a b
You are Arpit

22/09/2012 Mirafra Confidential 117


Demo – 3

 Write a script to split a given file into a specified number


of files.

22/09/2012 Mirafra Confidential 118


REF

Here document (1)

 Works as double quoted string.


$a=60; Output:
print<<xyz; He said he ran
He said he ran 50
50 meters
meters\n
$a 60
xyz He said he ran
print "He said he ran"

Note: Label must follow directly after the arrows with no spaces between.

22/09/2012 Mirafra Confidential 119


REF

Here document (2)

 To execute shell commands or scripts..


print <<`EOC`;
echo hi there
echo lo there
date
EOC

Output:
hi there
lo there
Wed Aug 29 04:07:32 PDT 2012

22/09/2012 Mirafra Confidential 120


REF

Here document (3)

 To execute shell commands or scripts (this doesn’t work)


print <<EOC;
echo hi there
echo lo there
date
EOC

Output:
echo hi there
echo lo there
date

22/09/2012 Mirafra Confidential 121


Next Session

 File Input/Output
 Functions/subroutines
 Regular expressions (Strength of PERL)

22/09/2012 Mirafra Confidential 122


Q/A

22/09/2012 Mirafra Confidential 123


Thanking Note

 Thanks to Seema for organizing this training.


 Thanks to Hemant & Moorthy for preparing/validating
the lab material.

22/09/2012 Mirafra Confidential 124


Thank You

22/09/2012 Mirafra Confidential 125

You might also like