+ All Categories
Home > Documents > LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open, ...

LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open, ...

Date post: 26-Dec-2015
Category:
Upload: regina-byrd
View: 214 times
Download: 1 times
Share this document with a friend
21
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong
Transcript
Page 1: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

LING/C SC/PSYC 438/538

Lecture 5Sandiway Fong

Page 2: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

Today’s Topics

• File input/output– open, <>

• References• Perl modules• Homework 2: due next Monday by midnight– email: [email protected]– one PDF file– subject: 438/538 homework 2

Page 3: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

File I/O

Files: must be opened for reading “<“ or writing “>” (overwrite or append mode “>>”)Shell syntax: I/O redirection “<“ “>”Opening a file creates a file handle (Perl variable) – not to be confused with filenameSupply the file handle for read/write

• Step 1: call open()

Page 4: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

File I/O

• Step 2: use the <> operator:

$in is the file handle instantiated by the open() call

Page 5: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

File I/O

• Line by line:

open($txtfile, $ARGV[0]) or die "$ARGV[0] not found!\n";while ($line = <$txtfile>) { print "$line";}close($txtfile)

the command $line = <$txtfile> inside the condition reads in a line from the filereferenced by the file handle $txtfile and places that line into the variable $line (including the newline at the end of the line) At the end of the file, $line is just an empty string (equivalent to false).the filename is the first parameter to the perl program (arguments go in @ARGV).

Page 6: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

More complex data structures

• Arrays and hashes may only contain scalars

• Question: How to accomplish nesting, i.e. put non-scalars inside?

• Answer: use references (pointers), which happen to be scalars

• http://perldoc.perl.org/perlreftut.html

(actually a reference is just an unsigned number: computer address)

Page 7: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

References

• Two ways to make a reference:Remember bracketing when initializing:( ) List – can be used for both arrays and hashes[] Reference to an array{} Reference to a hash

Page 8: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

References

• Example: array of arrays

Note: uses Make Rule 2

• Let’s figure out what the following mean:

de-reference

Page 9: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

References

• Looping (using for/foreach) with array/hash references:

Be careful! $aref->[3] and $aref[3] are different

Page 10: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

References

• Code:$a = [1, 2, 3, 4, 5];print $a+1

• What happens here?

Page 11: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

References

• Looping (using for/foreach) with array/hash references:

Be careful! $href->{‘red’} vs. $href{‘red’} are different.

Page 12: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

Experiment

• Unicode encoding (utf-8)

Page 13: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

Experiment• Note:

open pragmamost generalsolution

Page 14: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

Perl Modules

• CPAN: Comprehensive Perl Archive Network

sudo cpanm Unicode::CaseFold terminal command

Page 15: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

cpanm

• Ubuntu:

Ubuntu: sudo apt-get cpanminus

Page 16: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

cpanm

• OSX: assume command line tools have been installed– xcode-select --install

• uses program curl (cURL)– a command line tool for getting or sending files using URL syntax

bash-3.2$ which cpanmbash-3.curl -L http://cpanmin.us | perl - --sudo App::cpanminus % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 303 0 303 0 0 56 0 --:--:-- 0:00:05 --:--:-- 1553100 262k 100 262k 0 0 22916 0 0:00:11 0:00:11 --:--:-- 64374--> Working on App::cpanminusFetching http://www.cpan.org/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7000.tar.gz ... OKConfiguring App-cpanminus-1.7000 ... OKBuilding and testing App-cpanminus-1.7000 ... Password:OKSuccessfully installed App-cpanminus-1.70001 distribution installedbash-3.2$ which cpanm/usr/local/bin/cpanm

Page 17: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

Example of module use

Page 18: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

Homework 2

Question 1 (10pts)• Create a file data.txt (example):

1. Love for the the Bronx Bombers bubbled up in the absence of some local franchise.

2. On on on the table, we have some eggs.3. I have home sharing turned on, on both my computer and my apple tv.

• Write a Perl program that detects repeated words (many spell check/grammar programs can do this)

• Your program should read in data.txt and print a message stating the line number, the repeated word and its position if one exists. Example output:– perl repeated.perl data.txt– Line 1: word 3, “the” repeated 2 times– Line 2: word 1, “on” repeated 3 times

• Submit your program and examples of its output

Note: case

Page 19: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

Homework 2

• Useful functions:

chomp vs. chop

split a string into words

Note: multiple spaces ok with “ “ version

Page 20: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

Homework 2

• Question 2: describe how a repeated word program could stop flagging legitimate examples of repeated words in a sentence– Examples:• I wish that that question had an answer • Because he had had too many beers already, he skipped

the Friday office happy hour

Page 21: LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open,  References Perl modules Homework 2: due next Monday by midnight.

Homework 2

• Microsoft Word:


Recommended