Lecture 11: Perl - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Lecture 11: Perl

Description:

1. Lecture 11: Perl. Referring to and opening files. The ... ARGV[0] is the file gettysburg. 8. Opening a file ... open (FH,'$ARGV[0]') 9. Non-existent files ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 31
Provided by: ralphgr
Category:
Tags: argv | lecture | perl

less

Transcript and Presenter's Notes

Title: Lecture 11: Perl


1
Lecture 11 Perl
  • Referring to and opening files
  • The Perl match operator
  • Perl arrays and list data
  • Built-in Perl functions
  • Control-flow statements
  • Associative Arrays

2
The notion of a string
  • continent a new nation conceived in liberty
  • in this quote
  • the word in occurs once
  • the string in occurs twice
  • a string is a sequence of characters

3
The need for a string counter
  • grep Powell ap.stories
  • asked Powell. Powell replied that he would
  • grep returns the line with multiple Powells
  • tr -cs A-z0-9 1 sort uniq -c
  • 43 Powell
  • gives the answer but with a lot of extra
    unnecessary computing

4
A Perl Counter
  • Perl allows a scalar variable that can hold a
    numerical value lets call it ct
  • We can start ct with a value of 0
  • Each time our program finds Powell, it can add
    1 to the value of ct
  • At the end of the program, we can print the final
    value of ct, which will be the number of times
    it found the string Powell

5
Writing a Perl Program
  • Writing a program around the counter involves
    more than using the counter.
  • Lets step through a program that counts the
    number of instances of in in the Gettysburg
    Address

6
Filehandles
  • allow you to grab data
  • weve already used ltSTDINgt as a filehandle in
    name ltSTDINgt
  • now we want to associate a filename with a
    variable called FH (filehandle)
  • (FH, ARGV0)

7
Filehandles (2)
  • (FH, ARGV0)
  • we are writing a program called findwd.pl
  • the command line for this program will be
  • findwd.pl gettysburg
  • ARGV0 is the file gettysburg

8
Opening a file
  • In most programming languages, the program has to
    be told how to treat a file.
  • Is the file to be written to, read from, or
    opened for processing?
  • open (FH,"ARGV0")

9
Non-existent files
  • If Perl cant find the file that is to be opened
    we want to
  • stop the program from running
  • die
  • get a failure report
  • cannot open
  • Putting it together
  • die cannot open

10
How Perl reads from a file
  • Like sed, grep, etc., Perl reads a file line by
    line
  • So, ltFHgt will have the value of the current line
  • If we want to do text processing a line at a
    time, we can bind a variable in our program to
    the current value of ltFHgt
  • line ltFHgt

11
Getting a Perl script to read from a file
  • open (FH,"ARGV0") die "cannot open"
  • while (line ltFHgt)
  • open the file thats listed first on the command
    line and bind it to FH
  • or stop the program and report failure
  • bind the incoming line to the variable line
  • Q1,2

12
The operator
  • The equal sign binds a value to a variable
  • name ltSTDINgt
  • a fred
  • The match operator matches a pattern somewhere
    in a string
  • line /in/
  • line /Jjohn/ Q3,4

13
Counters in Perl
  • variables begin life with a dollar sign ct
  • numerical variables begin life with the value 1
  • an increment operator automatically adds 1 each
    time it is encountered ct
  • Q5

14
Lecture 12
  • Perl Arrays
  • Control-flow statements
  • Control flow foreach
  • Associative Arrays

15
Arrays in Perl
  • an array is a variable (.e.g, _at_line) that can
    hold a list
  • _at_line (four, score, and, seven)
  • each element in the list is a scalar variable
    that can be referenced by its place in the list
  • _at_line (four, score, and, seven)
  • line0 line1 line2 line3

16
Array Functions
  • _at_line(four,score,and,seven,
    years)
  • push(_at_line,ago)
  • xpop(_at_line)
  • unshift(_at_line,address)
  • xshift(_at_line)
  • _at_xreverse(_at_line)
  • _at_xsort(_at_line) Q6

17
The for statement
  • used to repeatedly execute a segment of code
  • for loop syntax
  • for (set_counter test_counter
    increment_counter)
  • action

18
The for loop counters
  • set_counter sets initial value for counter
  • test_counter states a condition tested at top
  • of loop
  • increment_counter
  • increments counter at
  • bottom of loop, just before
  • testing test_counter

19
Execution of a for loop
  • 1. the counter is set this is done once and only
    once
  • 2. test_counter is tested for a True/False answer
  • 3. if the result of test_counter is False
  • the for loop terminates
  • and the program drops to the action after the for
    loop
  • 4. if the result of test_counter is True
  • we execute the action
  • we increment the counter
  • we move to the next record

20
a for loop
  • !/usr/bin/perl -w
  • for (i1ilt5ii1)
  • print "i "
  • ii2
  • Q7

21
Control Structures in Perl
  • the if statement
  • if (name kermit
  • print Hello, name
  • the while statement
  • while (line ltFHgt
  • chop line
  • print line

22
Control Structures in Perl (2)
  • the for statement
  • for (i2ilt8ii2)
  • print i
  • (to increment by 1 on each loop, use the
    shorthand i)

23
Control Structures (3)
  • the foreach statement loops over each index in an
    array
  • _at_a (3,5,7,9)
  • foreach one (_at_a)
  • one 3
  • print one prints 9,15,21,27
  • Q7

24
Control Structures (3)
  • the foreach statement loops over each element in
    an array
  • !/usr/bin/perl -w
  • foreach.pl
  • _at_sentence (Here,we,go,again)
  • foreach word (_at_sentence)
  • print "word\n"

25
foreach.pl output
  • Here
  • we
  • go
  • again
  • Q8

26
Tabular Data
  • Name SSN Lang Sec
  • Gutierrez 095973624 Spanish 2A
  • Kim 497860227 Korean 3B
  • Boyer 853626857 French 3A
  • Masaka 353864257 Japanese 4C

27
Tabular Data in Linux
  • NameSSNLangLevel
  • Gutierrez095973624Spanish2A
  • Kim497860227Korean3B
  • Boyer853626857French3A
  • Masaka353864257Japanese4C

28
Accessing info from table-like data in Perl
  • in Perl
  • the notion of a row is captured by an array
  • _at_data
  • Gutierrez095973624Spanish2A
  • the notion of a table is captured by a hash
    data
  • Gutierrez095973624Spanish2A
  • Kim497860227Korean3B

29
Accessing info from table-like data in Perl (2)
  • info in the hash is accessed through a column, or
    key, in the data
  • _at_fields split(//,line)
  • key student0
  • the key is the 1st element of each row (the
    students last name in our data)

30
Accessing info from table-like data in Perl (3)
  • Other columns in the table are referenced with
    respect to this key, e.g.
  • langkey fields2
  • the language associated with a given key is in
    the 3rd field.
  • Q9,10
Write a Comment
User Comments (0)
About PowerShow.com