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

Similar documents
CS3157: Advanced Programming

PERL Scripting - Course Contents

IT441. Network Services Administration. Perl: File Handles

(Refer Slide Time: 01:12)

1. Introduction. 2. Scalar Data

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

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

Outline. CS3157: Advanced Programming. Feedback from last class. Announcement

Pathologically Eclectic Rubbish Lister

Hands-On Perl Scripting and CGI Programming

Perl. Perl. Perl. Which Perl

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

What is PERL?

Classnote for COMS6100

SEER AKADEMI LINUX PROGRAMMING AND SCRIPTINGPERL 7

CSCI-GA Scripting Languages

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

CS 230 Programming Languages

Regular expressions and case insensitivity

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

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

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

COMS 3101 Programming Languages: Perl. Lecture 2

Scripting Languages Perl Basics. Course: Hebrew University

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

Perl. Interview Questions and Answers

A Crash Course in Perl5

Language Reference Manual simplicity

CS3157: Advanced Programming. Outline

Fundamental of Programming (C)

COMS 3101 Programming Languages: Perl. Lecture 1

Learning Perl 6. brian d foy, Version 0.6, Nordic Perl Workshop 2007

UNIX Shell Programming

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Regular expressions and case insensitivity

Operators. Java operators are classified into three categories:

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

Operators in C. Staff Incharge: S.Sasirekha

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

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

T-( )-MALV, Natural Language Processing The programming language Perl

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

QUIZ: What value is stored in a after this

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

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Have examined process Creating program Have developed program Written in C Source code

Control Structures. CIS 118 Intro to LINUX

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

CSCI-GA Scripting Languages

CS 105 Perl: Completing the Toolbox

15.1 Origins and Uses of Ruby

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

print STDERR "This is a debugging message.\n";

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Perl Library Functions

Language Reference Manual

Perl for Biologists. Regular Expressions. Session 7. Jon Zhang. April 23, Session 7: Regular Expressions CBSU Perl for Biologists 1.

A shell can be used in one of two ways:

Introduction to C Programming

COMP322 - Introduction to C++ Lecture 01 - Introduction

File Handling in C. EECS 2031 Fall October 27, 2014

CS 241 Data Organization. August 21, 2018

Introduc)on to Unix and Perl programming

Client-Side Web Technologies. JavaScript Part I

The Design of C: A Rational Reconstruction (cont.)

We do not teach programming

On The First Steps. Chapter 1. In This Chapter

9.1 Origins and Uses of Perl

PHPoC vs PHP > Overview. Overview

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Review of the C Programming Language for Principles of Operating Systems

CS Final Exam Review Suggestions - Spring 2014

Princeton University COS 333: Advanced Programming Techniques A Subset of PHP

CMSC 331 Final Exam Fall 2013

Tcl/Tk for XSPECT a Michael Flynn

Bioinformatics. Computational Methods II: Sequence Analysis with Perl. George Bell WIBR Biocomputing Group

Full file at

UNIT- 3 Introduction to C++

Information Science 1

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

IT441. Network Services Administration. Data Structures: Lists

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Beginning Perl for Bioinformatics. Steven Nevers Bioinformatics Research Group Brigham Young University

Computer Science II Lecture 1 Introduction and Background

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Title:[ Variables Comparison Operators If Else Statements ]

Work relative to other classes

Admin. ! Assignment 3. ! due Monday at 11:59pm! one small error in 5b (fast division) that s been fixed. ! Midterm next Thursday in-class (10/1)

Beginning Perl. Third Edition. Apress. JAMES LEE with SIMON COZENS

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

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

Chapter 4 C Program Control

Ruby: Introduction, Basics

C Shell Tutorial. Section 1

Transcription:

Outline CS3157: Advanced Programming Lecture #2 Jan 23 Shlomo Hershkop shlomo@cs.columbia.edu Feedback Introduction to Perl review and continued Intro to Regular expressions Reading Programming Perl pg 1-45 Feedback from last class Good mix of computer science background Better board presentation Will move examples to laptop screen easier to follow and illustrate. You will need to let me know if you need more time to read something presented. Very varied skill set, a lot of programming experience and backgrounds Hardware Software Educational For those with high level of experience will have extra challenges in the lab and homeworks Last plug One of the points of computer science is to teach you how to think, learn, and analyze computational related information. Each course is a tool which you will collect for later use. Lots of tools in this course, since we will be covering many different topics and subjects. 1

Announcements First Lab next week Wednesday (2/1/06) Make sure you have access to your cs account Start reading Make sure you know where clic lab is located Will hold 2 lab sessions 1-3pm 3-5pm Office hours Will be posted later today on the webpage Please feel free to stop by to ask for help/advice/hints TA s will hold office hours in the TA room (1 st floor in mudd) My office hours are in my office (460 CSB) Conventions Something.pl version: >perl v Location: >which perl First line of script Linux: Windows: #!c:\perl\bin comment lines Hash (#) to the end of the line Can make the perl script executable (chmod +x command). scalars ($) arrays (@) hashes (%) subroutine(&) typeglob(*) Data types 2

Starts with $ $first $course int, real, string 234-89 36.34 hello world Context dependant $name = shlomo ; $name = 123; Scalars Arrays Starts with @ Order list of scalars @class3157 = ( shlomo, weijen, edward ); To reference elements, use the variable name with a dollar in front and subscript $class3157[0]; #is shlomo What do you think should: $class3157[-1]; $class3157[14]; Related to basic arrays Can get the length: $a = @class3157; print @class3157 Elements in the array $class3157[i] Refrencing an array $ref = \@class3157; De-refrencing a pointer $$ref[0] Hashes name/values pairs %phonelist = {adam=>718, barry=>345; or %phonelist = { adam,718, barry,345; Use the name to find the value $phonelist{ adam #is 718 Any other ideas for this? This can be done with any perl type Will print ARRAY(0x18328cc) when printing a referenced array 3

Variables II Programming statements Local my Global our local Special ALL LEX RO PKG simple statements are expressions that get evaluated they end with a semicolon (;) a sequence of statements can be contained in a block, delimited by braces ({ and ) the last statement in a block does not need a semicolon blocks can be given labels: myblock: { print "hello class\n"; Conditional Statements 1. simple if if (expression) {block else {block 2. unless unless (expression) {block else {block while for foreach Loops 3. compound if if (expression1) {block elsif (expression2) {block... elsif (expressionn) {block else {block 4

syntax: while (expression) {block example @b = (2,4,6,8); $a = @b; $i=0; while while ( $i < $a ) { print "i=",$i," b[i]=",$b[$i],"\n"; $i++; for syntax: for ( expression1; expression2; expression3 ) {block example: @b = (2,4,6,8); $a = @b; for ( $i=0; $i<$a; $i++ ) { print "i=",$i," b[i]=",$b[$i],"\n"; syntax: foreach var (list) {block example: @b = (2,4,6,8); $a = @b; foreach foreach $e (@b) { print "e=",$e,"\n"; Controlling loops next within a loop allows you to skip the current loop iteration last allows you to end the loop test3.pl 5

Modifiers you can follow a simple statement by an if, unless, while or until modifier: statement if expression; statement unless expression; statement while expression; statement until expression; example: @b = (2,4,6,8); $a = @b; print "hello world!\n" if ($a < 10); print "hello world!\n" unless ($a < 10); #print "hello world!\n" while ($a < 10); print "hello world!\n" until ($a < 10); Operators you can follow a simple statement by an if, unless, while or until modifier: statement if expression; statement unless expression; statement while expression; statement until expression; example: @b = (2,4,6,8); $a = @b; print "hello world!\n" if ($a < 10); print "hello world!\n" unless ($a < 10); print "hello world!\n" until ($a < 10); #print "hello world!\n" while ($a < 10); Reserved variables there s a (long) list of global special variables... a few important ones: $_ = default input and pattern-searching string example: @b = (2,4,6,8); foreach (@b) { print $_,"\n"; Reserved II $/ = input record separator (default is newline) $$ = process id of the perl process running the script $< = real user id of the process running the script $0 = (0=zero) name of the perl script @ARGV = list of command-line arguments %ENV = hash containing current environment STDIN = standard input STDOUT = standard output STDERR = standard error 6

Operators unary: 1.! : logical negation 2. - : arithmetic negation 3. : bitwise negation arithmetic 1. +,-,*,/,% : as you would expect 2. ** : exponentiation relational 1. >, <=, <=, <= : as you would expect equality 1. ==,!= : as you would expect 2. <=> : comparison, with signed result: 3. returns -1 if the left operand is less than the right; 4. returns 0 if they are equal; 5. returns +1 if the left operand is greater than the right Operators II assignment, increment, decrement = +=, ++ -=, -- *=, **=, /=, %= &&=, = just like in C syntax for defining: sub name {block sub name (proto) {block Subroutine where proto is like a prototype, where you put in sample arguments syntax for calling: name(args); name args; (the & sign is optional if you use parenthesis in the method call) any arguments passed to a subroutine come in as the array @_ $_[0], $_[1], etc Can also use the shift operator to move variables Since get a list of scalers, arrays and hashes need to be passed by references $n = 45; print "n is now $n\n"; testsub($n); print "n is now $n\n"; sub testsub{ $a = shift; print "in testsub $a\n"; $a++; Passing by value 7

$n = 45; print "n is now $n\n"; testsub(\$n); print "n is now $n\n"; sub testsub{ $a = shift; print "in testsub $a\n"; $$a++; Pass by reference Working with files open( FILEHANDLE, filename ); : to open a file for reading open( FILEHANDLE, >filename ); : to open a file for writing open( FILEHANDLE, >>filename ); : to open a file for appending use warn print "message"; or die print "message"; for error checking print FILEHANDLE,...; close( FILEHANDLE ); example: open( MYFILE,">a.dat" ); print MYFILE "hi there!\n"; print MYFILE "bye-bye\n"; close( MYFILE ); Built in functions chomp $var chomp @list removes any line-ending characters chop $var chop @list removes last character chr number returns the character represented by the ASCII value number eof filehandle returns true if next read on filehandle will return end-of-file exists $hash{$key returns true if specified hash key exists, even if its value is undefined exit exits the perl process immediately #!c:\perl\bin ($first,$last) = &getname(); print "First is $first"; Sample #1 #return the fill name as a string sub getname(){ return "shlomo hershkop"; #return name split sub getname(){ return ("shlomo","hershkop"); 8

Example II open( MYFILE2,"b.dat" ) warn "file not found!"; open( MYFILE2,"a.dat" ) die "file not found!"; while ( <MYFILE2> ) { print "$_\n" close( MYFILE2 ); More built in getc filehandle reads next byte from filehandle index string, substr [, start] returns position of first occurrence of substr in string, with optional starting position; also rindex which is index in reverse opendir dirhandle, dirname opens a directory for processing, kind of like a file; use readdir and closedir to process split /pattern/, string [, limit] splits string into a list of substrings, by finding delimiters that match pattern; example: split /([-,])/,"1-10,20"; returns (1, -, 10,,, 20) substr string, pos [, n, replacement] returns substring in string starting with position pos, for n characters Pragmas Compiler hints to allow you to operate in some special mode Will talk about later, but for now will discuss use warning use strict Strict mode This isn t about the midterm Tells perl to only allow variable you explicitly create in your programs Prevents typos Easier to maintain Less work for interpreter Will clearly state what it thinks you need to be doing to get things correct 9

Perl References there are lots and lots of advanced and funky things you can do in perl; this is just a start! here s a quick start reference: http://www.comp.leeds.ac.uk/perl/ http://www.perl.com function reference list is here: http://www.perldoc.com/perl5.6/pod/perlfunc.htm l Regular Expressions simplest regular expression is a literal string complex regular expressions use metacharacters to describe various options in building a pattern. \. ^ $ * +? {... [... ] (... ) escapes the character immediately following it matches any single character except newline matches at the beginning of a string matches at the end of a string matches the preceding element 0 or more times matches the preceding element 1 or more times matches the preceding element 0 or 1 times specifies a range of occurrences for the element preceding it matches any one of the class of characters in the brackets groups expressions (pipe) matches either the expression before or after it Basic The most basic match is: $string =~ m/sought_text/; Will return true if sought_text is part of string, false otherwise Perl assume m/???/ when use /???/ #!c:\perl\bin $name = "shlomo hershkop"; if($name =~ /lom/){ print "have found match\n"; else{ print "no match found\n"; What about? $name = "shlomo hershkop"; if($name =~ m/^her/){ print "have found match\n"; else{ print "no match found\n"; 10

Basic II Pattern attributes = binds a scalar to a pattern match, substitution or translation Will match case sensitive unless told not to by matching operators If($name =~ /shlomo/i ){ something! just like above, except that the return value is negated in the logical sense operators: m/pattern/gimosx : match g = match globally (all instances) i = do case insensitive matching note that first m is optional s/pattern/replacement/egimosx : search e = evaluate right side as an expression g = match globally (all instances) i = do case insensitive matching y/pattern1/pattern2/cds : translate c = complement pattern1 d = delete found but unreplaced characters s = squash duplicate replaced characters $s = "hello world"; print $s=[,$s,"]\n"; if ($s = m/x/) { print "there s an x in ",$s,"\n" else { print "there isn t\n" if ($s = m/l/i) { print "there s an l in ",$s,"\n" else { print "there isn t\n" output: $s=[hello world] there isn t there s an l in hello world Example $s = "hello world"; print $s=[,$s,"]\n"; $t = ($s = s/l/x/g); print $t=[,$t,"]\n"; print $s=[,$s,"]\n"; output: $s=[hello world] $t=[3] $s=[hexxo worxd] Example 2 11

Example 3 Next time $s = "hello world"; print $s=[,$s,"]\n"; $u = ($s = y/l/o/c); print $u=[,$u,"]\n"; print $s=[,$s,"]\n"; output: $s=[hello world] $u=[8] $s=[oollooooolo] Get book Do Reading (see schedule page). Read up on regular expressions Get some perl practice 12