Unix - Jazi Eko Istiyanto

download report

Transcript Unix - Jazi Eko Istiyanto

Chapter 9: Perl Programming
Practical Extraction and Report
Language
Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition
Learning to Use Perl
Objectives
After studying this lesson, you should be able to:
• Learn the basics of the Perl language
• Learn three data types
– Scalars
– Arrays
– Associative Arrays
• Basic control flows
Introduction to Perl
• Perl (Practical Extraction and Report
Language) was created in 1986 by Larry
Wall as a simple report generator
• Perl provides the best of several worlds
– Powerful and flexible, similar to a high-level
programming language such as C.
– An interpreted language. no interpreter, or
compiler needed.
– Provides all the features of the script
languages sed and awk, plus features not
found in either of these two languages.
What can Perl do?
•
•
•
•
•
•
anything involving text processing
internet programming
system administration
bioinformatics
quick prototyping
database programming
What is bad of Perl?
• Bad at efficiency
• Not suitable for large scale
computation
A simple program
• repeat.pl
#!/usr/bin/perl
$inputline = <STDIN>;
print( $inputline );
• chmod u+x repeat.pl
• Note: <STDIN> represents a line of
input from the standard input file.
Identifying Data Types
• Data may be represented in a Perl
program in a variety of ways
• You will learn about three basic types of
data:
Scalars
Arrays
Associated Arrays
• There are others, but we’ll talk about them
at a later time….
Data Types and Operations
Scalars
Arrays
Associative Arrays
Scalars
• A scalar is a simple variable that holds a
number or a string
• In C/C++, many different kinds of scalars,
such as int, float, double, char, bool
• In Perl, scalar variable can hold all these
types, without declaration
• Scalar variable names begin with a dollar
sign ($)
Scalar Names
• Scalar variable names begin with a dollar
sign ($)
• Next character is a letter
• Remaining characters: letters, numbers, or _
• Variable names can be between 1 and 251
characters in length
• Legal samples: $f, $bar, $z1, $d_3
• NOT legal: $, $_1, $47y, $x.y, $foo!
• Perl variables are case-sensitive.
Assign Values to Scalars
Examples:
$var = <STDIN>; # get input from screen
$var = 5 + 6 * 4;
$var = 3.458;
$var = “HUSKER";
$var = ‘Hello World!’;
Data Types and Operations
Scalars
Arrays
Associative Arrays
Arrays
• Arrays are variables that store an
ordered list of scalar values that are
accessed with numeric subscripts,
starting at zero.
• An “at” sign (@) precedes the name
of an array when assigning it values
Arrays
• A array can hold a list of any number or type of
scalars:
@array = ();
@array = (23, “HUSKER”, 3.14159);
@array = ($var1, $var2);
• Because Perl uses @ and $ to distinguish array
variables from scalar variables, the same name
can be used in an array variable and in a scalar
variable. For example:
$var = 1;
@var = (11, 27.1, "a string");
Accessing an Element in an Array
Index of array begins with zero
@array = (1, 2, 3, “HUSKER”);
$array[3] = 5; # (1, 2, 3, 5)
$scalar = $array[1]; # $scalar = 2;
$index = 2; $scalar = $array[$index]; # use a scalar
variable as a subscript; $scalar = 3;
Notes:
@array = (1, 2, 3, 4);
$scalar = $array[4]; # scalar will be a null string
(which is equivalent of zero)
$array[7] = 7; # array will be (1, 2, 3, 4, “”, “”, “”, 7)
$scalar = $array[-1]; # scalar will be a null string
Length of array
Retrieving the Length of a List:
$scalar = @array;
• Example:
@list = (“UNL", “HUSKER", “university");
$length = @list;
# $length is 3.
grep operation on array
• grep: extract the elements of a list that
match a specified pattern
foundlist = grep (pattern, searchlist);
• Example:
@list = (“UNL", “HUSKER", “university");
@foundlist = grep(/^[Uu]/, @list);
# foundlist = (“UNL”, “university”);
Push/Pop on Array
• Push: add an element to the end of a list
@array = ("one", "two");
$array[3] = "four"; # ("one", "two", "", "four ");
push (@array, "five"); # ("one", "two", "", "four",
"five");
• Pop: remove the last element from the end
of a list
$popped = pop (@array);
# popped is "five"; array is ("one", "two", "", "four ");
shift and unshift operations
• shift: remove an element from the front of a
list
@array = (1, 2, 3);
$firstval = shift(@array);
# mylist = 1, $array = ("2", "3")
• unshift: undo the effect of a shift function
@array = (1, 2, 3);
$num = unshift (@array, "newitem");
# returns the number of elements in the resulting list.
array = ("newitem", 1, 2, 3)
split operation
split: split a string into a list of elements
list = split (pattern, string);
Example 1:
$line = "This:is:a:string";
@array = split (/:/, $line);
#array is ("This", "is", "a", "string").
Example 2:
@array = split(/[\s]+/, $line2); # split according to
white space.
sort operation
sort: sorts the elements of an array in
alphabetical order and returns the sorted list
list2 = sort (list1);
Example:
@array = ("this", "is", "a", "test");
@array2 = sort (@array);
#array2 is ("a", "is", "test", "this").
Note: sort treats its items as strings, not integers; items
are sorted in alphabetical, not numeric, order.
@array = (70, 100, 8);
@array = sort (@array);
# array is (100, 70, 8), not (8, 70, 100)
reverse operation
reverse: reverses the order of the elements of
a list or array variable, and returns the
reversed list.
list2 = reverse (list1);
Example:
@array = ("backwards", "is", "array", "this");
@array2 = reverse(@array);
#array2 is ("this", "array", "is", "backwards")
Data Types and Operations
Scalars
Arrays
Associative Array
Associative Array
• An associative array is a variable that represents
a set of key/value pairs
• Instead of indexing by numbers as we did in
arrays, we can look up the values by name.
• Why need it? A easy data structure keeps
mapping.
– Host name, ip address
– ip address, hostname
– Student name, score
– Driver’s license number, name
Associative Array
• Associative arrays are preceded by a percent sign (%)
when they are assigned values.
For example:
%fruit = ("apples“ => 6, "cherries“ => 8, "oranges“ => 11);
$d = 2*$fruit{“apples”} +3* $fruit{“cherries”};
# $d now is 28.
Or: %fruit = ("apples", 6, "cherries", 8, "oranges", 11);
Or: @fruit = ("apples", 6, "cherries", 8, "oranges", 11);
%fruit = @fruit;
Associative Array
• List array indexes
%fruit = ("apples", 9, "bananas", 23, "cherries", 11);
@fruitsubs = keys(%fruits);
# @fruitsubs is ("apples", "bananas", "cherries");
• List array values
@fruitvalues = values(%fruits);
# @ fruitvalues is (9, 23, 11)
Basic Control Flow: if
• if statement
if (expr_1) {
statement_block_1
} elsif (expr_2) {
statement_block_2
} else {
default_statement_block
}
• Example:
if ($number) { print ("The number is not zero.\n"); }
Basic Control Flow: while loop
• while statement
while (expr) {
statement_block
}
• Example:
while ($done == 0) {
print ("The value of count is", $count, "\n");
if ($count == 3) { $done = 1; }
$count = $count + 1;
}
Basic Control Flow: for loop
• for statement
for (expr1; expr2; expr3) {
statement_block
}
• Example:
for ($count=0; $count < 5; $count++) {
# statements inside the loop go here
}
Basic Control Flow: foreach
• foreach statement
foreach localvar (listexpr) {
statement_block;
}
• Example:
@words = ("Here", "is", "a", "list.");
foreach $word (@words) {
print ("$word\n");
}
Summary
• Perl is being extensively used as a
powerful text-manipulation tool
• Perl has three basic data types:
– Scalars (begin with $)
– Arrays (begin with @)
– Associative Arrays (begin with %)
• A list is an ordered group of simple
variables or literals, separated by commas
Chapter Summary
• Basic logic control
– if
– while
– for
– foreach