CSCI-GA Scripting Languages

Similar documents
CSCI-GA Scripting Languages

1. Introduction. 2. Scalar Data

CS 5142 Scripting Languages

PERL Scripting - Course Contents

Beginning Perl. Mark Senn. September 11, 2007

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

Perl. Interview Questions and Answers

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

COMP284 Scripting Languages Lecture 3: Perl (Part 2) Handouts

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

Perl. Many of these conflict with design principles of languages for teaching.

Perl. Perl. Perl. Which Perl

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

COMS 3101 Programming Languages: Perl. Lecture 1

Lecture 2: Programming in Perl: Introduction 1

Pathologically Eclectic Rubbish Lister

More Perl. CS174 Chris Pollett Oct 25, 2006.

Chapter 1. Fundamentals of Higher Order Programming

Scripting Languages Perl Basics. Course: Hebrew University

Hands-On Perl Scripting and CGI Programming

Perl Tutorial. Diana Inkpen. School of Information Technology and Engineering University of Ottawa. CSI 5180, Fall 2004

What is PERL?

CS 105 Perl: Completing the Toolbox

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

Perl Library Functions

Outline. Introduction to Perl. Why use scripting languages? What is expressiveness. Why use Java over C

CSCI-GA Scripting Languages

Part 1: jquery & History of DOM Scripting

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

CSCI 4152/6509 Natural Language Processing Lecture 6: Regular Expressions; Text Processing in Perl

LECTURE 16. Functional Programming

COMS 3101 Programming Languages: Perl. Lecture 2

Introduction to Perl. Perl Background. Sept 24, 2007 Class Meeting 6

UNIX Shell Programming

I/O and Text Processing. Data into and out of programs

G Programming Languages - Fall 2012

Introduction to Perl Session 6. special variables subroutines Introduction to Perl

Ruby: Introduction, Basics

Bash scripting. Can put together bash commands into one big file to run all at once! CIS c. 22/02/09 Slide 1

Python I. Some material adapted from Upenn cmpe391 slides and other sources

8/13/ /printqp.php?heading=II BSc [ ], Semester III, Allied: COMPUTER PROGRAMMING-PERL -309C&qname=309C

GNU ccscript Scripting Guide IV

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

Indian Institute of Technology Kharagpur. PERL Part II. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

CS 105 Perl: Perl subroutines and Disciplined Perl

Programming Perls* Objective: To introduce students to the perl language.

Ruby: Introduction, Basics

... and run the script as /path/to/script.pl. Of course, it'll need to be executable first, so chmod 755 script.pl (under Unix).

Control Structures. Important Semantic Difference

Shells & Shell Programming (Part B)

Flow Control. CSC215 Lecture

Perl and Python ESA 2007/2008. Eelco Schatborn 27 September 2007

(Refer Slide Time: 01:12)

SEER AKADEMI LINUX PROGRAMMING AND SCRIPTINGPERL 7

Now applying :unique to lexical variables and to subroutines will result in a compilation error.

Arguments of the use overload directive are (key, value) pairs. For the full set of legal keys, see Overloadable Operations below.

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Functional Programming. Pure Functional Programming

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

VBA Handout. References, tutorials, books. Code basics. Conditional statements. Dim myvar As <Type >

Welcome to Research Computing Services training week! November 14-17, 2011

Regular expressions and case insensitivity

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

COMP284 Scripting Languages Lecture 2: Perl (Part 1) Handouts

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Ruby: Introduction, Basics

Perl Regular Expressions. Perl Patterns. Character Class Shortcuts. Examples of Perl Patterns

A control expression must evaluate to a value that can be interpreted as true or false.

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CMSC 331 Final Exam Fall 2013

Client-Side Web Technologies. JavaScript Part I

A Crash Course in Perl5

PERL MOCK TEST PERL MOCK TEST II

Shell Programming (bash)

Types, Operators and Expressions

Indian Institute of Technology Kharagpur. PERL Part III. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

CSCI 4152/6509 Natural Language Processing. Lab 3: Perl Tutorial 2

An introduction introduction to functional functional programming programming using usin Haskell

The e switch allows Perl to execute Perl statements at the command line instead of from a script.

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

CS 360 Programming Languages Interpreters

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

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

Chapter 6 Control Flow. June 9, 2015

Introduction to Perl programmation & one line of Perl program. BOCS Stéphanie DROC Gaëtan ARGOUT Xavier

The Environment Model. Nate Foster Spring 2018

Regular Expressions: The Power of Perl

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

Types, Operators and Expressions

Perl, Object Orientation

CSE 130 [Winter 2014] Programming Languages

This page covers the very basics of understanding, creating and using regular expressions ('regexes') in Perl.

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Using References to Create Complex Structures. The array and hash composers

Modularity and Reusability I. Functions and code reuse

Transcription:

CSCI-GA.3033.003 Scripting Languages 9/11/2013 Textual data processing (Perl) 1

Announcements If you did not get a PIN to enroll, contact Stephanie Meik 2

Outline Perl Basics (continued) Regular Expressions 3

Arrays Resizable Literals: list @a=(1,3,5), range @b=2..4 Indexing: e.g. $a[1] Zero-based; negative index counts from end $#a returns last index of @a, in this case, 2 Write to non-existent index auto-vivifies Free: undef @a, truncate: $#a=1 Array slice: using multiple indices, e.g., @a[0,2] or @a[1..2] Using array in scalar context: returns length scalar(@a); # 3 = size 4

Hashes Associative arrays, using hash tables Literals: none; use list literals instead %h = (lb => 1, oz => 16, g => 453); Indexing: string, bare-word quotes optional $h{"oz"}, $h{oz} Write to non-existing index auto-vivifies Free: undef %h; delete: undef $h{oz}; Hash slice: returns array, e.g., @a = @h{lb, g}; returns (1, 453) Using hash in scalar context: returns string scalar(%h); # 2/8"=buckets/capacity 5

Array and Hash References References are scalars, only scalars can be stored in other arrays/hashes Literals: anonymous array/hash composers $ar=[1,3,5];, $hr={x=>2,y=>0}; Indexing: e.g., $ar->[1]; $hr->{y}; Dereference operator -> optional in nested chain, e.g., $rarh->[123]{abc} Write to non-existent index auto-vivifies all indices on the way Using reference in string context: returns string describing the type and address print $ar; # "ARRAY(0x18bef54)" 6

Data Structures $recipe = [ { qty => 2, unit => "cup", ing => "flour", }, { qty => 4, unit => "oz", ing => "sugar", }, { qty => 3, unit => "tbsp", ing => "milk", }, ]; $recipe : array : hash qty = 2 unit = "cup" ing = "flour" : hash qty = 4 unit = "oz" ing = "sugar" : hash qty = 3 unit = "tbsp" ing = "milk" 7

Control Statements if(expr){ } [elsif(expr){ }] [else{ }] Proper statements (man perlsyn) Modifiers Operators (man perlfunc) [label:] while(expr){ } [continue{ }] [label:] until(expr){ } [continue{ }] [label:] for(expr; expr; expr) { } [label:] foreach [var] (list) { } [continue{ }] [label:] for [var] (list) { } [continue{ }] [label:] { } [continue{ }] stmt (if unless while until foreach) expr do eval sub continue goto last next redo return die dump exit 8

What is Truth Strings "" and "0" are false, everything else is true File read < > returns "" at end-of-file Number 0 converted to string "0" Array in scalar context 0 iff empty Hash in scalar context "0" iff empty Reference "" iff undef 9

Writing Subroutines Declaration sub [id] [proto] [attrs] [{ }] No id: anonymous; no proto: list operator; no block: declaration without definition To return a value: return expr; If no explicit return: value of last expression Arguments Array @_ Call-by-reference ($_[i] is alias of actual i) Common idiom: copy arguments into named locals, e.g., my ($a,$b) = @_; 10

Concepts Blocks as Function Arguments do{ }, eval{ }, sub{ } look like control statements, but aren t They are functions with block argument Other examples: grep, map, sort You can write such functions yourself This is a lightweight form of callback SmallTalk language does same, only more SmallTalk has no control statements at all Method on boolean with block argument (x<y) iftrue:[mx:=y] iffalse:[mx:=x] 11

Default Variable $_ Only angle operator in while condition: read line from file handle into $_ No variable in foreach: use $_ as iteration variable No binding in pattern match: use $_ No explicit operand for certain functions: use $_, e.g., print, chop, chomp, split, Don t confuse $_ with @_ (arguments) 12

Outline Perl Basics (continued) Regular Expressions 13

Example of m// Matching Input Script Output -rw-r--r-- 1 soule soule 3998 Aug 29 12:49 hw01.html -rw-r--r--@ 1 soule soule 4480 Sep 5 14:10 hw02.html -rw------- 1 soule soule 8868 Sep 9 12:09 index.html #!/usr/bin/perl -w while($line = <>) { } if ($line =~ m/(\d+)\s\w{3}[\d\s:]+(\s+)$/) { } printf "$2 contains $1 bytes\n"; hw01.html contains 3998 bytes hw02.html contains 4480 bytes index.html contains 8868 bytes 14

Explanation of m// Matching =~: binding operator string to search in true if matched m//: match operator (): grouping+capturing if ($line =~ m/(\d+)\s\w{3}[\d \s:]+(\s+)$/) { printf "$2 contains $1 bytes\n"; } $1, $2: captured groups (\d+)\s\w{3}[\d \s:]+(\s+)$: regular expression 15

Inside a Regular Expression Kind Construct Syntax Example Pattern Matches Essentials Character Itself b b Concatenation e 1 e 2 bc bc Alternative (or) e 1 e 2 a bc a, bc Repetition ( 0) e* a*, a, aa, aaa Grouping (e) (a b)c ac, bc Quantifier Optional e? (a b)?, a, b Repetition ( 1) e+ a+ a, aa, aaa Repetition e{n} a{3} aaa Char class Custom class [ ] [a-c] a, b, c Wildcard character.. a, 3, : Shortcut class \ \d 0,1,2,,9 Assertion Zero-width anchor $,^,\b, \b (word boundary) 16

Explanation of s/// substitution string to search and modify =~: binding operator #!/usr/bin/perl -w while($line = <>) { if ($line =~ s/.+\s(\d+)\s\w{3}[\d\s:]+(\s+)$/$2 contains $1 bytes/) { print $line; } } s///: substitution operator true if matched.+\s(\d+)\s\w{3}[\d \s:]+(\s+)$: regular expression $2 contains $1 bytes: replacement text 17

Outside a Regular Expression Match: m// or simply //; substitute: s/// Binding: =~ positive,!~ negative Or when missing: bound to $_ Modifiers: e.g. s///g global substitute Return value: true if success, invert for!~ Or when assigned to list: groups, e.g., my ($a,$b) = $s =~ /(\w+) (\d+)/; Output: $& entire match, $1, groups Find out more: man perlre 18

Library Functions String: chomp, chop, length, substr, uc RegExp: m//, s///, split Math: abs, cos, exp, sin, sqrt Array: pop, push, shift, unshift Control: die, do, eval, last, next, sub Scoping: local, my, our, package, use Files: chdir, chmod, fork, printf, qx// And many more ( man perlfuc ) 19

Perl Documentation Included: manpages perl, perldata, perlsyn, perlop, perlre, perlfunc, Book: Programming Perl, 3rd edition. Larry Wall, Tom Christiansen, and Jon Orwant. O Reilly, 2000 [safari]. Online: CPAN = Comprehensive Perl Archive Network, http://www.cpan.org http://www.perl.com 20

Administrative Last Slide Today s lecture Associative arrays Regular expressions Basics of Perl Next lecture Context Typeglobs Object-oriented Perl 21