180 likes | 191 Views
LING/C SC/PSYC 438/538. Lecture 6 Sandiway Fong. Adminstrivia. A note on Windows 10 PowerShell for Perl Coercion (again) Conditionals Looping Sorting in Perl and Python. PowerShell (Windows). PowerShell (Windows). Finish Python install. Four versions present:
E N D
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong
Adminstrivia • A note on Windows 10 PowerShell for Perl • Coercion (again) • Conditionals • Looping • Sorting in Perl and Python
Finish Python install Four versions present: Python command line (2.7.x) IDLE Python (2.7.x) Python 3.6 IDLE Python 3.6
Implicit Coercion Note: qw = quote word 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 • Conditionals • 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?
ARGV Command line: perlargv.perl 1 2 3
Perl Arrays • by default sort works according to the ASCII chart, character by character asciitable.com
Perl Arrays: sorting Numeric sort? • Idea: to pass an inline comparison function as parameter… • Note: fc (fold case) use feature 'fc';
Python: sorting • $ python3 -itest.py • >>> items • ['A', 'a', 'B', 'b', 'C', 'c'] • >>> items.sort() • >>> items • ['A', 'B', 'C', 'a', 'b', 'c'] • >>> items.sort(reverse=True) • >>> items • ['c', 'b', 'a', 'C', 'B', 'A'] • >>> items.sort(reverse=True, key=str.lower) • >>> items • ['c', 'C', 'b', 'B', 'a', 'A'] • >>> Using method (not function) .sort()