Download presentation
Presentation is loading. Please wait.
Published byCorey Davidson Modified over 6 years ago
1
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong
2
Today Topics Algorithmic thinking: a worked example Perl References
Perl Modules: cpanm Reminder: first programming homework due tomorrow at midnight!
3
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 of a student who would be able and interested? Spencer Young
4
Case study: Repeated Word Detection
Microsoft Word:
5
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
6
Case study: Repeated Word Detection
Generalize specific example, then code it up Array indices start from 0… w1 w2 w3 … wn-1 wn 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
7
Case study: Repeated Word Detection
8
Case study: Repeated Word Detection
9
Case study: Repeated Word Detection
10
Case study: Repeated Word Detection
11
Case study: Repeated Word Detection
12
Case study: Repeated Word Detection
13
Case study: Repeated Word Detection
a decent first pass …
14
Case study: Repeated Word Detection
Sample data file: Output:
15
Case study: Repeated Word Detection
Second try.. merging multiple occurrences Sample data file: Output:
16
Case study: Repeated Word Detection
Second try.. merging multiple occurrences
17
Case study: Repeated Word Detection
Third try.. implementing a simple table of exceptions
18
Case study: Repeated Word Detection
Third try.. table of exceptions Sample data file: Output: Grammar, very, very, very big error
19
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 perlreftut.html (actually a reference is just an unsigned number: computer address)
20
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:
21
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
22
References Looping (using for/foreach) with array/hash references:
Careful! $aref->[3] and $aref[3] are different
23
References Perl code: What happens here? $a = [1, 2, 3, 4, 5];
print $a+1, "\n"; What happens here?
24
References Looping (using for/foreach) with array/hash references:
Careful! $href->{‘red’} and $href{‘red’} are different.
25
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 -o cpanm chmod +x cpanm cpan cpanp
26
CPAN Minus Ubuntu: Ubuntu: sudo apt-get cpanminus
27
Where are Perl modules stored?
@INC array variable perl -e 'print "$_\n" /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
28
Example: a day of week calculator
dow.perl perl dow.perl Can't locate Date/Calc.pm (you may need to install the Date::Calc module) 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.
29
Example: a day of week calculator
sudo cpanm Date::Calc Password: --> Working on Date::Calc Fetching OK Configuring Date-Calc OK ==> Found dependencies: Bit::Vector, Carp::Clan --> Working on Bit::Vector Fetching OK Configuring Bit-Vector OK ==> Found dependencies: Carp::Clan --> Working on Carp::Clan Fetching OK Configuring Carp-Clan OK ==> Found dependencies: Test::Exception --> Working on Test::Exception Fetching OK Configuring Test-Exception OK ==> Found dependencies: Sub::Uplevel --> Working on Sub::Uplevel Fetching OK Configuring Sub-Uplevel OK Building and testing Sub-Uplevel OK Successfully installed Sub-Uplevel Building and testing Test-Exception OK Successfully installed Test-Exception-0.43 Building and testing Carp-Clan OK Successfully installed Carp-Clan-6.06 Building and testing Bit-Vector OK Successfully installed Bit-Vector-7.4 Building and testing Date-Calc OK Successfully installed Date-Calc distributions installed perldoc -lm Date::Calc /opt/local/lib/perl5/site_perl/5.24/Date/Calc.pm
30
Example: a day of week calculator
perl dow.perl usage: month day year perl dow.perl /12/2017 falls on a Tuesday perl dow.perl /12/2016 falls on a Monday
31
localtime Date::Calc also Now() Built-in: all numbers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.