LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong
Administrivia Homework 5 graded Terminal log from last time Homework 5 review Quick Homework 6 (Heads Up: Homework 7 coming on Thursday) Perl Hashes and Python dicts
Terminal log from last time Desktop$ perl -le "@a = qw-a aa aaa A AA AAA ( ) { }-; print \"@a\"" a aa aaa A AA AAA ( ) { } Desktop$ perl -le "@a = qw-a aa aaa A AA AAA ( ) { }-; @b = sort @a; print \"@b\"" ( ) A AA AAA a aa aaa { } Desktop$ perl -le "@a = qw-a aa aaa A AA AAA ( ) { }-; @b = sort {\$a cmp \$b} @a; print \"@b\"" Desktop$ perl -le "@a = qw-0 1 2 01 10 100 ( ) { }-; @b = sort {\$a <=> \$b} @a; print \"@b\"" 0 ( ) { } 1 01 2 10 100
Terminal log from last time Desktop$ perl -le "@a = qw-a b c A B C-; use feature fc; @b = sort {fc(\$a) cmp fc(\$b)} @a; print \"@b\"" a A b B c C Desktop$ perl -le "@a = qw-A B C a b c-; use feature fc; @b = sort {fc(\$a) cmp fc(\$b)} @a; print \"@b\"" A a B b C c Desktop$ perl -le "@a = qw-0 5 3 2 1 99 a A-; @b = sort @a; print \"@b\"" 0 1 2 3 5 99 A a Desktop$ perl -le "@a = (0, 5, 3, 2, 1, 99, 'a', 'A'); @b = sort @a; print \"@b\"" Desktop$
Homework 5 Review Perl Resources: sub https://perldoc.perl.org/perlsub.html substr https://perldoc.perl.org/functions/substr.html length https://perldoc.perl.org/functions/length.html if-then-else https://perldoc.perl.org/perlsyn.html#Compound-Statements
Homework 5 Review palindrome.py Desktop$ python3 pallindrome.py dad True Desktop$ python3 pallindrome.py dada False Desktop$ python3 pallindrome.py racecar Desktop$
Homework 5 Review Let's write the Perl program! length($word) substr($word, -1) substr EXPR,OFFSET,LENGTH substr($word, 0, 1) substr($word, 1, -1) or substr($word, 1, length($word)-2)
Homework 5 Review (Bonus) One student tried '好不好' (good-NOT-good => OK?), but the palindrome code didn't work. Chinese tag questions: 对不对 true (or not)? 有没有 exists (or not)? 是不是 yes (or no)? Perl pre-dates Unicode: and Unicode support is not turned on by default for backwards compatibility. perl -C [number/list] A the @ARGV elements are expected to be strings encoded in UTF-8. O STDOUT will be in UTF-8. I STDIN is assumed to be in UTF-8. S I + O + E.
Quick Homework 6 Due tomorrow night: reviewed on Thursday Question 1: what's the difference between a) and b)? my @a = 4 x 4; my @a = (4) x 4; read https://perldoc.perl.org/functions/split.html Question 2: what does split do here for a) vs. b)? my @a = split " ", 'this is a sentence.'; my @a = split //, 'this is a sentence.'; Due tomorrow night: reviewed on Thursday
Perl Hashes dict in Python Note: Perl array: @ […] Perl hash: % {…}
no quotes needed for strings Perl Hashes Notes on arrays and hashes arrays are indexed using integers hashes are like arrays with user-defined indexing (aka associative array or hash table or dictionary (Python)) initialization (use list notation (or shortcut): round brackets and commas) @a = ("zero", "one", "two", "three", "four"); %h = ("zero", 0, "one", 1, "two", 2, "three", 3, "four", 4); (key/value pairs) access to individual elements (square brackets vs. curly braces) $a[1] “one” $h{zero} 0 Shortcut: no quotes needed for strings 'zero' "zero"
Perl Hashes Notes on arrays and hashes output print @a zeroonetwothreefour print "@a" zero one two three four print %h three3one1zero0two2four4 (note: different order) print "%h" %h (literal, no interpolation done)
More on Hash tables Looping over arrays: Output:
More on Hash tables Exists: Unique key constraint: exists $fruitColor{apple} exists $fruitColor{orange}
Dictionaries (dict) Lists and tuples are indexed by whole numbers (from 0) Dictionaries are indexed by a key (usually a string) and return some value associated with the key Note: use of curly braces Dictionaries are not ordered (like sets) – see next slide Methods keys(), values(), items() Refer to key + value as an item: encoded as a tuple
Dictionaries (dict)
Python dict Dictionary order preservation depends on the version of Python used … Python 3.6
Python dict list comprehension
Python dict How to print the contents of a dictionary? Use a for-loop and method items(): k,v is a tuple
Python dict All values are lists Advantage: simplifies the code Trade-off Values are lists or a string Advantage: simpler-looking dict
Python dict Works too! Less transparent: relies on a Python-particular quirk …
Python dict function zip() pairs up elements from two lists into an iterable
Python dict function zip() doesn't always work quite the same …
Python list ranges Perl has a range operator: .. less powerful in some ways, more powerful in others https://perldoc.perl.org/perlop.html#Range-Operators