LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong
Today's Topics Perl data type coercion, conditions and looping More on Perl hashes and Python dicts Homework 2
Implicit Coercion Example: Example: Perl features implicit coercion of data types Example: the following program prints Equal! == is the numeric equality comparison operator my $one_string = "1"; my $one_number = 1; if ($one_string == $one_number) { print "Equal!\n" } else { print "Different!\n" } Example: the following program prints 3 is my number . is the string concatenation operator my @a = qw(one, two, three); my $string = @a." is my number"; print "$string\n";
Implicit Coercion print "4" * 4 16 print "4" x 4 (“x” is the repetition operator) 4444 @a = (4) x 4 (in list context) (4, 4, 4, 4)
Conditionals and Looping if ( @a < 10 ) { print “Small array\n” } else {print “Big array\n” } Note: @a here is a scalar = size of array unless (@a > 10) { print “@a\n” } Note: if size of array a is ≤ 10, it prints the contents of array a Ungraded Homework Exercise: look up the equivalents in Python (www.python.org) do they always exist?
Conditionals and Looping
Conditionals and Looping Looping over arrays %fruits = qw(apple green orange orange lemon yellow); foreach $f (keys %fruits) { print $f, " => ", $fruits{$f}, "\n” } gives output: lemon => yellow apple => green orange => orange Note: keys %fruits = ("lemon", "apple", "orange") is an array
ARGV Command line: perl argv.perl 1 2 3
General Looping
More on Perl hash tables Example: Output: Unique key constraint:
Python 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
Python 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-particuar quirk …
Python dict function zip() pairs up elements from two (or more) lists into an iterable
Python dict How to get that Perl list data structure format into a Python dict?
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
Python dict function zip() doesn't always work quite the same in all versions …
Homework 2 Q1: Perl. Q2: Perl. Q3: Python. Q4: Python. What does @a = 4 x 4 do? Q2: Perl. @l1 = ("a", "e", "i", "o" , "u"); @l2 = ("あ", "え", "い", "お", "う"); write a program that builds a hash that maps romaji to hiragana, e.g. $h{i} should be "い" Give examples to show your program works Hint: use a loop and shift (or pop) Q3: Python. Do the same for l1 and l2 in Python using zip. Q4: Python. Do the same, without zip, using list comprehensions
Homework 2 Instructions: email me submit everything in one PDF file! subject of email: course number, your name, homework 2 cite any discussion or source due date: Monday 10th by midnight