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

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
LING/C SC/PSYC 438/538 Lecture 4 9/1 Sandiway Fong.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Practical Extraction & Report Language Picture taken from
Guide To UNIX Using Linux Third Edition
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Examining the Code [Reading assignment: Chapter 6, pp ]
Recitation 1 Programming for Engineers in Python.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Programming for Linguists An Introduction to Python 24/11/2011.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Introduction to Perl Giorgos Georgakilas Graduated from C.E.I.D.Graduated from C.E.I.D. M.Sc. degree in ITMBM.Sc. degree in ITMB Ph.D. student in DIANA-LabPh.D.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Linux Administration Working with the BASH Shell.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
C-Shell with Functions
Input from STDIN STDIN, standard input, comes from the keyboard.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CSc 120 Introduction to Computer Programing II Adapted from slides by
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Introduction to Python
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 2 Sandiway Fong.
CSE 303 Concepts and Tools for Software Development
Introduction to Python
Conditionals (if-then-else)
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
Perl Variables: Array Web Programming.
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong.
Chapter 7: Using Functions, Subs, and Modules
Programming Project #1 Command Shell
Programming Project #1 Fork and Command Shell
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
CSE 303 Concepts and Tools for Software Development
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
Lab 4: Introduction to Scripting
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
Presentation transcript:

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

Administrivia Terminal log from last lecture Arrays in Perl Homework 5

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$

Terminal log from previous class ~$ python3 Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) [Clang 6.0 (clang-600.0.57)] 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 = ('a', 'b', 'c', 'd', 'e') File "<stdin>", line 1 @a = ('a', 'b', 'c', 'd', 'e') ^ IndentationError: unexpected indent >>> a = ('a', 'b', 'c', 'd', 'e') >>> a[0] 'a'

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

Shell quoting Bash shell (MacOS, Linux): cf. Windows 10 slide earlier manual: http://www.gnu.org/software/bash/manual/ can't write: perl -e '@a=(\'a\',\'b\',\'c\'); print "@a\n"'

Shell quoting Bash shell (MacOS, Linux): can write: perl -e "@a=(\"a\",\"b\",\"c\"); print \"@a\n\""

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: sort @ARRAY; reverse @ARRAY,; push @ARRAY, $ELEMENT; pop @ARRAY; shift @ARRAY; unshift @ARRAY, $ELEMENT, splice @ARRAY, $OFFSET, $LENGTH, $ELEMENT $ELEMENT above can be @ARRAY 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)

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

perlintro: Perl Arrays

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]" https://en.wikipedia.org/wiki/Sator_Square

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!

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.

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

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

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

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

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

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

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

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

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

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

Homework 5 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 Start with: Map the Python code line-by-line into Perl:

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]:

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