Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong."— Presentation transcript:

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

2 Administrivia Terminal log from last lecture Arrays in Perl Homework 5

3 Terminal log from previous class
~$ cd Desktop Desktop$ perl ex.perl My name is Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at ex.perl line 2. Execution of ex.perl aborted due to compilation errors. My name is John My name is $name Desktop$ perl ex.perl My name is \$name\nDesktop$ perl test.perl b a b c d e Desktop$ perl test.perl abcde a-b-c-d-e 4 5 Desktop$

4 Terminal log from previous class
~$ python3 Python (v3.7.3:ef4ec6ed12, Mar , 16:52:21) [Clang 6.0 (clang )] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("My name is", name) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'name' is not defined >>> name = "Mary" My name is Mary >>> print("My name is ", name) My name is Mary >>> print("My name is" + name) My name isMary = ('a', 'b', 'c', 'd', 'e') File "<stdin>", line = ('a', 'b', 'c', 'd', 'e') ^ IndentationError: unexpected indent >>> a = ('a', 'b', 'c', 'd', 'e') >>> a[0] 'a'

5 Terminal log from previous class
>>> a[1] 'b' >>> a[-1] 'e' >>> a[-2] 'd' >>> b = ['a', 'b', 'c', 'd', 'e'] >>> b[-2] >>> b[1] >>> b[1] = 'B' >>> b ['a', 'B', 'c', 'd', 'e'] >>> a[1] = 'B' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> b ['a', 'B', 'c', 'd', 'e'] >>> b[-1] 'e' >>> len(b) 5 >>> len(b)-1 4

6 Shell quoting Bash shell (MacOS, Linux): cf. Windows 10 slide earlier
manual: can't write: perl -e print

7 Shell quoting Bash shell (MacOS, Linux): can write:
perl -e print

8 perlintro: Perl Arrays
like a simple ordered list… (in Python, we use a list) Coercion @ARRAY = number of elements in scalar context Python (no coercion): len(array) Built-in functions: $ELEMENT; $ELEMENT, $OFFSET, $LENGTH, $ELEMENT $ELEMENT above can Python: array.sort(), array.reverse(), NO push (use array.append() instead), array.pop(), No shift/unshift etc… Built-in arrays: @ARGV (command line arguments) @_ (sub(routine) arguments)

9 perlintro: Perl Arrays
Similar to pop/push, but operates at the left end of the array Python doesn't have these defined but can be simulated via slicing and concatenation: array[1:] list + array

10 perlintro: Perl Arrays

11 Homework 5: introduction
Palindrome word: a word that spells the same backwards or in reverse (case insensitive) e.g. Dad, radar, racecar from Herculaneum (buried in AD 79): Word English POS/Features Root SATOR sower NN AREPO Arepo NNP TENET holds VBZ (3SG.PRES) tenere OPERA work NNS (NOM/ACC.PL) opus ROTAS wheels NNS (ACC.PL) rota "The farmer Arepo has [as] works wheels [a plough]"

12 Homework 5: introduction
Suppose we represent a word as a string (Python or Perl). How do we check whether a word is a palindrome? Two ideas: Compare a word with its reverse for identity. An empty string or string with one letter is a palindrome. A string (> 1 letter) is a palindrome if the first and last letters match, and the rest of the string is also a palindrome. Example: racecar 'racecar' == reverse('racecar') (='racecar') r[aceca]r -> a[cec]a -> c[e]c -> e ok!

13 Homework 5: introduction
Using Python reversed() and Perl reverse(): Notes: eq compares strings; == compares numbers @ARGV are the arguments passed on the command line to the program @ARGV is an array (historical naming convention) $ARGV[0] is the first argument $ARGV[1] would be the second, etc. Booleans in Perl: 0 or '' or '0' is False, any non-zero number, e.g. 1, is True.

14 ARGV ARGV: argument vector Python: sys.argv
first popularized through the C programming language (Unix). command line: program_name arg1 arg2 arg3 … some differences in what ARGV[0], ARGV[1], etc. map to, in some cases ARGV[0] = program_name, in others ARGV[0] = arg1. Python: sys.argv import sys sys.argv[0] sys.argv[1] python3 prog.py arg1 arg2

15 ARGV prog.py: import sys print(len(sys.argv)) print(sys.argv) Desktop$ python3 prog.py arg1 arg2 3 ['prog.py', 'arg1', 'arg2'] Desktop$

16 ARGV prog.perl: Desktop$ perl prog.perl arg1 arg2 1 arg1 arg2 Desktop$
use warnings; use strict; print "$#ARGV\n"; print Desktop$ perl prog.perl arg1 arg2 1 arg1 arg2 Desktop$

17 Homework 5: introduction
Using Python reversed() and Perl reverse(): Notes: reversed() creates an iterator/iterable, ''.join(iterable)

18 Homework 5: introduction
Using Python reversed() and Perl reverse(): Notes: reversed() creates an iterator/iterable, ''.join(iterable)

19 Homework 5: introduction
Python also has a (non-transparent) slicing operator:

20 Homework 5: introduction
Python also has a (non-transparent) slicing operator:

21 Homework 5 Your homework is to implement idea #2 in Perl: Two ideas:
Compare a word with its reverse for identity. An empty string or string with one letter is a pallindrome. A string (> 1 letter) is a pallindrome if the first and last letters match, and the rest of the string is also a pallindrome. Example: racecar 'racecar' == reversed(racecar)='racecar' r[aceca]r -> a[cec]a -> c[e]c -> e ok! d[ad]a

22 Homework 5 palindrome.py Desktop$ python3 pallindrome.py dad True Desktop$ python3 pallindrome.py dada False Desktop$ python3 pallindrome.py racecar Desktop$

23 Homework 5 Your Perl program should behave similarly… Note:
in Perl, 0 is False, non-zero True. @_ is the array used in subroutine parameter passing. Therefore $_[0] is the 1st argument passed to sub palindrome

24 Homework 5 Perl Resources: sub https://perldoc.perl.org/perlsub.html
substr length if-then-else

25 Homework 5 Start with: Map the Python code line-by-line into Perl:

26 Homework 5 Q1: submit your Perl code and sample runs. If necessary, e.g. if your mapping is not straightforward, explain your Perl code. Q2: modify your Perl code to check for the presence of $ARGV[0], i.e. it should print an error message and exit gracefully in this situation: Q3: modify the Python code also to gracefully check for sys.argv[1]:

27 Homework 5 Due date next Monday midnight Usual rules: ONE PDF file
We'll review the code in class next week this time


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

Similar presentations


Ads by Google