chapter 9: perl programming practical extraction and report language some materials are taken from...

32
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Upload: beverly-palmer

Post on 04-Jan-2016

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Chapter 9: Perl Programming

Practical Extraction and Report Language

Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Page 2: 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 PerlObjectives

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

Page 3: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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.

Page 4: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

What can Perl do?

• anything involving text processing

• internet programming

• system administration

• bioinformatics

• quick prototyping

• database programming

Page 5: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

What is bad of Perl?

• Bad at efficiency

• Not suitable for large scale computation

Page 6: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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.

Page 7: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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:ScalarsArraysAssociated Arrays

• There are others, but we’ll talk about them at a later time….

Page 8: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Data Types and Operations

ScalarsArraysAssociative Arrays

Page 9: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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 ($)

Page 10: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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.

Page 11: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Assign Values to Scalars

Examples:

$var = <STDIN>; # get input from screen

$var = 5 + 6 * 4;

$var = 3.458;

$var = “HUSKER";

$var = ‘Hello World!’;

Page 12: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Data Types and Operations

ScalarsArraysAssociative Arrays

Page 13: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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

Page 14: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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");

Page 15: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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

Page 16: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Length of array

Retrieving the Length of a List:

$scalar = @array;

• Example:

@list = (“UNL", “HUSKER", “university");

$length = @list;

# $length is 3.

Page 17: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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”);

Page 18: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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 ");

Page 19: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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)

Page 20: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

split operation

split: split a string into a list of elementslist = 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.

Page 21: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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)

Page 22: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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")

Page 23: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Data Types and Operations

ScalarsArraysAssociative Array

Page 24: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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

Page 25: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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;

Page 26: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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)

Page 27: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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"); }

Page 28: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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;

}

Page 29: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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

}

Page 30: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Basic Control Flow: foreach

• foreach statementforeach localvar (listexpr) { statement_block;}

• Example:

@words = ("Here", "is", "a", "list.");

foreach $word (@words) {

print ("$word\n");

}

Page 31: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

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

Page 32: Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition

Chapter Summary

• Basic logic control– if

– while

– for

– foreach