LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.

Slides:



Advertisements
Similar presentations
LING/C SC/PSYC 438/538 Lecture 4 9/1 Sandiway Fong.
Advertisements

I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
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 Computational Linguistics Sandiway Fong Lecture 4: 8/30.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Continuing with Perl Homework 3: first Perl homework – due Sunday by midnight – one PDF file, by .
LING/C SC/PSYC 438/538 Lecture 2 Sandiway Fong. Today’s Topics Did you read Chapter 1 of JM? – Short Homework 2 (submit by midnight Friday) Today is Perl.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong. Administrivia Homework 2 graded.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong. Adminstrivia Homework 4 not yet graded …
LING 408/508: Programming for Linguists Lecture 18 November 2 nd.
LING 408/508: Programming for Linguists Lecture 5 September 9 th.
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong. Homework 4 Submit one PDF file Your submission should include code and sample runs Due date Monday 21.
LING/C SC/PSYC 438/538 Online Lecture 7 Sandiway Fong.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Chapter 14: Sequential Access Files
UMBC CMSC 104 – Section 01, Fall 2016
Input from STDIN STDIN, standard input, comes from the keyboard.
Chapter 7 Text Input/Output Objectives
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Scripting March 1st, 2004 Class Meeting 7.
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
stdin, stdout, stderr Redirection
LING/C SC/PSYC 438/538 Lecture 2 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.
CSE 303 Concepts and Tools for Software Development
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
Lecture 4B More Repetition Richard Gesick
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
LING 408/508: Computational Techniques for Linguists
LING 581: Advanced Computational Linguistics
LING/C SC 581: Advanced Computational Linguistics
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 10 Programming Fundamentals with JavaScript
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 9 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong.
LING 408/508: Computational Techniques for Linguists
LING/C SC/PSYC 438/538 Lecture 10 Sandiway Fong.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong.
Using Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CSE 303 Concepts and Tools for Software Development
Lecture 5: Functions and Parameters
LING 388: Computers and Language
LING/C SC 581: Advanced Computational Linguistics
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong.
CSE 303 Concepts and Tools for Software Development
Summary of what we learned yesterday
Data Structures & Algorithms
CSC 352– Unix Programming, Fall 2012
Perl Programming Dr Claire Lambert
EECE.2160 ECE Application Programming
LING/C SC 581: Advanced Computational Linguistics
Lecture Topics: 11/20 HW 7 What happens on a memory reference Traps
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong.
Presentation transcript:

LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong

Adminstrivia Homework 3 graded Remember: Submit PDF file Always submit the program Always submit sample runs Explanation appreciated!

Adminstrivia I'm away next week and the following Monday. Procedure: Next week there will be lecture slides posted online And one homework Following Monday class canceled

Today’s Topics File input/output References Perl modules open, <> References Perl modules

File I/O Step 1: call open() 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 filename Supply the file handle for read/write

File I/O Step 2: use the <> operator: $in is the file handle instantiated by the open() call

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 file referenced 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).

Useful string functions chomp vs. chop To split a string into words: Note: multiple spaces ok with " " variant

Worked Example Read names and frequencies into a hash from a file

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)

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 Two ways to make a reference:

References Example: array of arrays Note: uses Make Rule 2: square brackets Let’s figure out what the following mean: de-reference

References Looping (using for/foreach) with array/hash references: Be careful! $aref->[3] and $aref[3] are different

References Code: $a = [1, 2, 3, 4, 5]; print $a+1 What happens here?

References Looping (using for/foreach) with array/hash references: Be careful! $href->{‘red’} vs. $href{‘red’} are different.

Experiment Unicode encoding (utf-8)

Experiment Note: open pragma most general solution

Perl Modules CPAN: Comprehensive Perl Archive Network

cpanm Ubuntu: Ubuntu: sudo apt-get cpanminus

cpanm OSX: assume command line tools have been installed xcode-select -p /Applications/Xcode.app/Contents/Developer xcode-select –install xcode-select: error: command line tools are already installed, use "Software Update" to install updates uses program curl (cURL) a command line tool for getting or sending files using URL syntax bash-3.2$ which cpanm bash-3.curl -L http://cpanmin.us | perl - --sudo App::cpanminus --> Working on App::cpanminus Fetching http://www.cpan.org/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7000.tar.gz ... OK Configuring App-cpanminus-1.7000 ... OK Building and testing App-cpanminus-1.7000 ... Password: OK Successfully installed App-cpanminus-1.7000 1 distribution installed /usr/local/bin/cpanm

Example of module use last year

Example of module use Missing Perl Module: Can't locate Date/Calc.pm in @INC (@INC contains: /opt/local/lib/perl5/site_perl/5.16.3/darwin-thread-multi-2level /opt/local/lib/perl5/site_perl/5.16.3 /opt/local/lib/perl5/vendor_perl/5.16.3/darwin-thread-multi-2level /opt/local/lib/perl5/vendor_perl/5.16.3 /opt/local/lib/perl5/5.16.3/darwin-thread-multi-2level /opt/local/lib/perl5/5.16.3 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl .) at dow.perl line 1. BEGIN failed--compilation aborted at dow.perl line 1.

Example of module use Install Perl Module sudo cpanm Date::Calc --> Working on Date::Calc Fetching http://www.cpan.org/authors/id/S/ST/STBEY/Date-Calc-6.4.tar.gz ... OK Configuring Date-Calc-6.4 ... OK ==> Found dependencies: Bit::Vector --> Working on Bit::Vector Fetching http://www.cpan.org/authors/id/S/ST/STBEY/Bit-Vector-7.4.tar.gz ... OK Configuring Bit-Vector-7.4 ... OK Building and testing Bit-Vector-7.4 ... OK Successfully installed Bit-Vector-7.4 (upgraded from 7.3) Building and testing Date-Calc-6.4 ... OK Successfully installed Date-Calc-6.4 (upgraded from 6.3) 2 distributions installed /Library/Perl/5.18 and /usr/local/bin

localtime Code: all numbers