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

Slides:



Advertisements
Similar presentations
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Advertisements

LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an from me.
LING/C SC/PSYC 438/538 Lecture 5 9/8 Sandiway Fong.
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 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 3 8/30 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong. Administrivia Homework 2 graded.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong. Adminstrivia Homework 4 not yet graded …
An Overview of Perl A language for Systems and Network Administration and Management: An overview of the language.
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.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 6: References suggested reading: perlreftut
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
CMPT 120 Topic: Python’s building blocks -> More Statements
3.1 Fundamentals of algorithms
Arrays: Checkboxes and Textareas
Intermediate Perl - References
Matlab Training Session 4: Control, Flow and Functions
Topics Introduction to Repetition Structures
Exceptions, Templates, and the Standard Template Library (STL)
Be A programmer in Steps
Functions and Procedures
Numeric Arrays Numeric Arrays Chapter 4.
The Linux Operating System
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 2 Sandiway Fong.
Error Handling Summary of the next few pages: Error Handling Cursors.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
PDO Database Connections: Getting data out of the database
While Loops BIS1523 – Lecture 12.
LING 408/508: Computational Techniques for Linguists
LING 581: Advanced Computational Linguistics
LING/C SC 581: Advanced Computational Linguistics
Advanced Topics Web Programming.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Geography 375 Introduction to Python May 15, 2015
The week of October 30th – November 3rd
PDO Database Connections: Getting data out of the database
Selection CIS 40 – Introduction to Programming in Python
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
Chapter 2: The Linux System Part 2
LING/C SC/PSYC 438/538 Lecture 9 Sandiway Fong.
In Class Program: Today in History
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong.
CISC124 Labs start this week in JEFF 155.
LING/C SC/PSYC 438/538 Lecture 18 Sandiway Fong.
UMBC CMSC 104 – Section 01, Fall 2016
LING/C SC/PSYC 438/538 Lecture 26 Sandiway Fong.
Standard Version of Starting Out with C++, 4th Edition
Programming Control Structures with JavaScript Part 2
Exceptions, Templates, and the Standard Template Library (STL)
Mastering Memory Modes
LING/C SC 581: Advanced Computational Linguistics
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong.
EECE.2160 ECE Application Programming
Loops and Arrays in JavaScript
Summary of what we learned yesterday
Data Structures & Algorithms
BIT116: Scripting Lecture 6 Part 1
EECE.2160 ECE Application Programming
Creating your game.
EECE.2160 ECE Application Programming
Getting Started…. What’s First? Student Homework Assignment
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.
Presentation transcript:

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

Today Topics Algorithmic thinking: a worked example Perl References Perl Modules: cpanm Reminder: first programming homework due tomorrow at midnight!

RFPerl… I am a 3rd year doctoral student in the Accounting department. I am looking for a graduate student that is able to write a simple perl script for me. After speaking with individuals who are familiar with coding, it is my understanding that my task is a simple task for someone who is proficient at perl. I am coauthoring a project with a professor in the department and I can offer compensation to the student. Could you send me an email of a student who would be able and interested? Spencer Young spencery@email.arizona.edu

Case study: Repeated Word Detection Microsoft Word:

Case study: Repeated Word Detection Key: think algorithmically… think of a specific example first w1 w2 w3 w4 w5 Compare w1 with w2 Compare w2 with w3 Compare w3 with w4 Compare w4 with w5

Case study: Repeated Word Detection Generalize specific example, then code it up Array indices start from 0… w1 w2 w3 … wn-1 wn array: @words Compare w1 with w1+1 words0 ,words1 … wordsn-1 Compare w2 with w2+1 “for” loop implementation … Compare wn-2 with wn-2+1 Array indices end just before $#words… Compare wn-1 with wn

Case study: Repeated Word Detection

Case study: Repeated Word Detection

Case study: Repeated Word Detection

Case study: Repeated Word Detection

Case study: Repeated Word Detection

Case study: Repeated Word Detection

Case study: Repeated Word Detection a decent first pass …

Case study: Repeated Word Detection Sample data file: Output:

Case study: Repeated Word Detection Second try.. merging multiple occurrences Sample data file: Output:

Case study: Repeated Word Detection Second try.. merging multiple occurrences

Case study: Repeated Word Detection Third try.. implementing a simple table of exceptions

Case study: Repeated Word Detection Third try.. table of exceptions Sample data file: Output: Grammar, very, very, very big error

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 Let’s figure out what the following mean: 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: Careful! $aref->[3] and $aref[3] are different

References Perl code: What happens here? $a = [1, 2, 3, 4, 5]; print $a+1, "\n"; What happens here?

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

CPAN Minus https://www.cpan.org Various installers for Perl modules: cpanm (CPAN Minus) install (various ways) cd ~/bin (a directory where you want it to be) curl -L https://cpanmin.us/ -o cpanm chmod +x cpanm cpan cpanp

CPAN Minus Ubuntu: Ubuntu: sudo apt-get cpanminus

Where are Perl modules stored? @INC array variable perl -e 'print "$_\n" foreach @INC' /opt/local/lib/perl5/site_perl/5.24/darwin-thread-multi-2level /opt/local/lib/perl5/site_perl/5.24 /opt/local/lib/perl5/vendor_perl/5.24/darwin-thread-multi-2level /opt/local/lib/perl5/vendor_perl/5.24 /opt/local/lib/perl5/5.24/darwin-thread-multi-2level /opt/local/lib/perl5/5.24 . perldoc -lm Lingua::EN::CMUDict (from lecture2) /opt/local/lib/perl5/site_perl/5.24/Lingua/EN/CMUDict.pm

Example: a day of week calculator dow.perl perl dow.perl Can't locate Date/Calc.pm in @INC (you may need to install the Date::Calc module) (@INC contains: /opt/local/lib/perl5/site_perl/5.24/dar win-thread-multi-2level /opt/local/lib/perl5/site_perl/5.24 /opt/local/lib/perl5/vendor_perl/5.24/d arwin-thread-multi-2level /opt/local/lib/perl5/vendor_perl/5.24 /opt/local/lib/perl5/5.24/darwin- thread-multi-2level /opt/local/lib/perl5/5.24 .) at dow.perl line 1. BEGIN failed--compilation aborted at dow.perl line 1.

Example: a day of week calculator sudo cpanm Date::Calc Password: --> 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, Carp::Clan --> 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 ==> Found dependencies: Carp::Clan --> Working on Carp::Clan Fetching http://www.cpan.org/authors/id/K/KE/KENTNL/Carp-Clan-6.06.tar.gz ... OK Configuring Carp-Clan-6.06 ... OK ==> Found dependencies: Test::Exception --> Working on Test::Exception Fetching http://www.cpan.org/authors/id/E/EX/EXODIST/Test-Exception-0.43.tar.gz ... OK Configuring Test-Exception-0.43 ... OK ==> Found dependencies: Sub::Uplevel --> Working on Sub::Uplevel Fetching http://www.cpan.org/authors/id/D/DA/DAGOLDEN/Sub-Uplevel-0.2800.tar.gz ... OK Configuring Sub-Uplevel-0.2800 ... OK Building and testing Sub-Uplevel-0.2800 ... OK Successfully installed Sub-Uplevel-0.2800 Building and testing Test-Exception-0.43 ... OK Successfully installed Test-Exception-0.43 Building and testing Carp-Clan-6.06 ... OK Successfully installed Carp-Clan-6.06 Building and testing Bit-Vector-7.4 ... OK Successfully installed Bit-Vector-7.4 Building and testing Date-Calc-6.4 ... OK Successfully installed Date-Calc-6.4 5 distributions installed perldoc -lm Date::Calc /opt/local/lib/perl5/site_perl/5.24/Date/Calc.pm

Example: a day of week calculator perl dow.perl usage: month day year perl dow.perl 9 12 2017 9/12/2017 falls on a Tuesday perl dow.perl 9 12 2016 9/12/2016 falls on a Monday

localtime Date::Calc also Now() Built-in: all numbers