Chapter 9: Perl Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 9: Perl Programming

Description:

Title: Unix Author: Nathan Ullger Last modified by: H Created Date: 11/6/1999 4:03:20 AM Document presentation format: On-screen Show Company: Cyborg Computing systems – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 33
Provided by: Natha139
Category:

less

Transcript and Presenter's Notes

Title: Chapter 9: Perl Programming


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
2
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

3
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.

4
What can Perl do?
  • anything involving text processing
  • internet programming
  • system administration
  • bioinformatics
  • quick prototyping
  • database programming

5
What is bad of Perl?
  • Bad at efficiency
  • Not suitable for large scale computation

6
A simple program
  • repeat.pl
  • !/usr/bin/perl
  • inputline ltSTDINgt
  • print( inputline )
  • chmod ux repeat.pl
  • Note ltSTDINgt represents a line of input from the
    standard input file.

7
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 well talk about them at a
    later time.

8
Data Types and Operations
  • Scalars
  • Arrays
  • Associative Arrays

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

10
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.

11
Assign Values to Scalars
  • Examples
  • var ltSTDINgt get input from screen
  • var 5 6 4
  • var 3.458
  • var HUSKER"
  • var Hello World!

12
Data Types and Operations
  • Scalars
  • Arrays
  • Associative Arrays

13
Arrays
  • Arrays are variables that store an ordered list
    of scalar values that are accessed with numeric
    subscripts, starting at zero.
  • An at sign (_at_) precedes the name of an array
    when assigning it values

14
Arrays
  • A array can hold a list of any number or type of
    scalars
  • _at_array ()
  • _at_array (23, HUSKER, 3.14159)
  • _at_array (var1, var2)
  • Because Perl uses _at_ 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
  • _at_var (11, 27.1, "a string")

15
Accessing an Element in an Array
  • Index of array begins with zero
  • _at_array (1, 2, 3, HUSKER)
  • array3 5 (1, 2, 3, 5)
  • scalar array1 scalar 2
  • index 2 scalar arrayindex use a
    scalar variable as a subscript scalar 3
  • Notes
  • _at_array (1, 2, 3, 4)
  • scalar array4 scalar will be a null
    string (which is equivalent of zero)
  • array7 7 array will be (1, 2, 3, 4, ,
    , , 7)
  • scalar array-1 scalar will be a null
    string

16
Length of array
  • Retrieving the Length of a List
  • scalar _at_array
  • Example
  • _at_list (UNL", HUSKER", university")
  • length _at_list
  • length is 3.

17
grep operation on array
  • grep extract the elements of a list that match a
    specified pattern
  • foundlist grep (pattern, searchlist)
  • Example
  • _at_list (UNL", HUSKER", university")
  • _at_foundlist grep(/Uu/, _at_list)
  • foundlist (UNL, university)

18
Push/Pop on Array
  • Push add an element to the end of a list
  • _at_array ("one", "two")
  • array3 "four" ("one", "two", "", "four
    ")
  • push (_at_array, "five") ("one", "two", "",
    "four", "five")
  • Pop remove the last element from the end of a
    list
  • popped pop (_at_array)
  • popped is "five" array is ("one", "two", "",
    "four ")

19
shift and unshift operations
  • shift remove an element from the front of a list
  • _at_array (1, 2, 3)
  • firstval shift(_at_array)
  • mylist 1, array ("2", "3")
  • unshift undo the effect of a shift function
  • _at_array (1, 2, 3)
  • num unshift (_at_array, "newitem")
  • returns the number of elements in the resulting
    list. array ("newitem", 1, 2, 3)

20
split operation
  • split split a string into a list of elements
  • list split (pattern, string)
  • Example 1
  • line "Thisisastring"
  • _at_array split (//, line)
  • array is ("This", "is", "a", "string").
  • Example 2
  • _at_array split(/\s/, line2) split
    according to white space.

21
sort operation
  • sort sorts the elements of an array in
    alphabetical order and returns the sorted list
  • list2 sort (list1)
  • Example
  • _at_array ("this", "is", "a", "test")
  • _at_array2 sort (_at_array)
  • array2 is ("a", "is", "test", "this").
  • Note sort treats its items as strings, not
    integers items are sorted in alphabetical, not
    numeric, order.
  • _at_array (70, 100, 8)
  • _at_array sort (_at_array)
  • array is (100, 70, 8), not (8, 70, 100)

22
reverse operation
  • reverse reverses the order of the elements of a
    list or array variable, and returns the reversed
    list.
  • list2 reverse (list1)
  • Example
  • _at_array ("backwards", "is", "array", "this")
  • _at_array2 reverse(_at_array)
  • array2 is ("this", "array", "is", "backwards")

23
Data Types and Operations
  • Scalars
  • Arrays
  • Associative Array

24
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
  • Drivers license number, name

25
Associative Array
  • Associative arrays are preceded by a percent sign
    () when they are assigned values.
  • For example
  • fruit ("apples gt 6, "cherries gt 8,
    "oranges gt 11)
  • d 2fruitapples 3 fruitcherries
  • d now is 28.
  • Or fruit ("apples", 6, "cherries", 8,
    "oranges", 11)
  • Or _at_fruit ("apples", 6, "cherries", 8,
    "oranges", 11)
  • fruit _at_fruit

26
Associative Array
  • List array indexes
  • fruit ("apples", 9, "bananas", 23, "cherries",
    11)
  • _at_fruitsubs keys(fruits)
  • _at_fruitsubs is ("apples", "bananas",
    "cherries")
  • List array values
  • _at_fruitvalues values(fruits)
  • _at_ fruitvalues is (9, 23, 11)

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

28
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

29
Basic Control Flow for loop
  • for statement
  • for (expr1 expr2 expr3)
  • statement_block
  • Example
  • for (count0 count lt 5 count)
  • statements inside the loop go here

30
Basic Control Flow foreach
  • foreach statement
  • foreach localvar (listexpr)
  • statement_block
  • Example
  • _at_words ("Here", "is", "a", "list.")
  • foreach word (_at_words)
  • print ("word\n")

31
Summary
  • Perl is being extensively used as a powerful
    text-manipulation tool
  • Perl has three basic data types
  • Scalars (begin with )
  • Arrays (begin with _at_)
  • Associative Arrays (begin with )
  • A list is an ordered group of simple variables or
    literals, separated by commas

32
Chapter Summary
  • Basic logic control
  • if
  • while
  • for
  • foreach
Write a Comment
User Comments (0)
About PowerShow.com