1 More Perl CIS*2450 Advanced Programming Concepts.

Slides:



Advertisements
Similar presentations
1/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules File I/O and Formatting Operators, Flow Control Statements Regular Expression Subroutines Hash.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.
Regular Expressions Regular Expression (or pattern) in Perl – is a template that either matches or doesn’t match a given string. if( $str =~ /hello/){
W3101: Programming Languages (Perl) 1 Perl Regular Expressions Syntax for purpose of slides –Regular expression = /pattern/ –Broader syntax: if (/pattern/)
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/28.
Guide To UNIX Using Linux Third Edition
Perl Programming WeeSan Lee
Scripting Languages Chapter 8 More About Regular Expressions.
UNIX Filters.
More on Regular Expressions Regular Expressions More character classes \s matches any whitespace character (space, tab, newline etc) \w matches.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Last Updated March 2006 Slide 1 Regular Expressions.
Practical Extraction & Report Language PERL Joseph Beltran.
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
An Introduction to Unix Shell Scripting
Perl. Perl notes 2 Perl Perl - Practical extraction report language –for text files –system management –combines C, SED, AWK, SH –interpreted –dynamic.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Shell Script & Perl Programming Lesson 9. Shell Programming A shell script is a text file that contains Linux (UNIX) commands, which you enter using any.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Perl (Tutorial II) NLP Course 07 Examples. EXAMPLE 1 #!/usr/bin/perl #Opens and reads the file "sentences" (input file) line by line. #For the i-th sentence.
Bioinformatics 生物信息学理论和实践 唐继军
1 Perl Syntax: control structures Learning Perl, Schwartz.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
 2003 Jeremy D. Frens. All Rights Reserved. Calvin CollegeDept of Computer Science(1/8) Regular Expressions in Java Joel Adams and Jeremy Frens Calvin.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Searching and Regular Expressions. Proteins 20 amino acids Interesting structures beta barrel, greek key motif, EF hand... Bind, move, catalyze, recognize,
Introduction to Unix – CS 21
Perl II Part III: Motifs and Loops. Objectives Search for motifs in DNA or Proteins Interact with users at the keyboard Write data to files Use loops.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Institute for Visualization and Perception Research 1 © Copyright 1998 Haim Levkowitz Perl primer... Syntax similar to C, various shell script langs. 3.
R EGULAR E XPRESSION IN P ERL (P ART 1) Thach Nguyen.
Chapter 11: Perl Scripting Off Larry’s Wall. In this chapter … Background Terminology Syntax Variables Control Structures File Manipulation Regular Expressions.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
© A B C D 78 A1A2A3A4 B1 B2 B3B4 C1C2 C3C4 D1D2D3D4 A5A6A7A8 B5B6B7B8 C5C6C7C8 D5D6D7D8 Questions: Ships Remaining: © 2012.
Part 4 Arrays: Stacks foreach command Regular expressions: String structure analysis and substrings extractions and substitutions Command line arguments:
The Scripting Programming Language
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Jigsaw cards for LARGE classes Use these cards for classes with 24 or more students. Distribute letter cards evenly. If you have 32 students, pass out.
Awk 2 – more awk. AWK INVOCATION AND OPERATION the "-F" option allows changing Awk's "field separator" character. Awk regards each line of input data.
COMP234-Perl Variables, Literals Context, Operators Command Line Input Regex Program template.
Shell Script & Perl Programming
Introduction to Bioinformatic Computation. Lecture #
Perl Programming Language Design and Implementation (4th Edition)
Shell Scripting March 1st, 2004 Class Meeting 7.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Perl Variables: Array Web Programming.
Control Structures: if Conditional
Pemrosesan Teks Herika Hayurani Sept 19, 2006
Regular Expressions and Grep
Introduction to Bioinformatic Computation. Lecture #
REGEX.
INTRODUCTION to PERL PART 1.
Presentation transcript:

1 More Perl CIS*2450 Advanced Programming Concepts

2 Shorthands in Perl \t \n \s \S \w \W \d \D Tab character Newline character Matches any whitespace character Anything not \s [a-zA-Z0-9_] use \w+ to match a word Anything not \w [0-9] a digit Anything not \d [^0-9]

3 Reading in a File #!/usr/bin/perl while ( $_ = ) { print $_; } #!/usr/bin/perl while ( <> ) { print; }

4 Invert.pl #!/usr/bin/perl # Usage:./invert.pl filename $file = # read in the 1 st command line argument $ret = open(FH,$file); # open the file whose name was on the cmd line $i = 0; $lines[$i++] = tell(FH); # read in all lines and store their locations while ( ) { $lines[$i++] = tell(FH); } $i--; while ( $i >= 0 ) { # read in lines from the bottom to top seek(FH,$lines[$i--],0); $outp = ; print $outp; # print out the line }

5 Nonum.pl #!/usr/bin/perl while ( $_ = ) { if ( /^[^0-9]+$/ ) { print $_; } I see 5 dogs I see five dogs./nonum.pl testfile ????????????

6 Nonum.pl #!/usr/bin/perl while ( $_ = ) { if ( /^[^0-9]+$/ ) { print $_; } I see 5 dogs I see five dogs./nonum.pl testfile I see five dogs

7 Nonum1.pl #!/usr/bin/perl while ( $_ = ) { if ( /[^0-9]+/ ) { print $_; } I see 5 dogs I see five dogs./nonum.pl testfile ??????????

8 Nonum1.pl #!/usr/bin/perl while ( $_ = ) { if ( /[^0-9]+/ ) { print $_; } I see 5 dogs I see five dogs./nonum.pl testfile I see 5 dogs I see five dogs

9 Nonum2.pl #!/usr/bin/perl while ( $_ = ) { $var = $_; chop $var; if ( $var =~ m/[^0-9]+/ ) { print $_; } I see 5 dogs I see five dogs./nonum.pl testfile ??????????

10 Nonum2.pl #!/usr/bin/perl while ( $_ = ) { $var = $_; chop $var; if ( $var =~ m/[^0-9]+/ ) { print $_; } I see 5 dogs I see five dogs./nonum.pl testfile I see 5 dogs I see five dogs

11 Nonum.pl #!/usr/bin/perl while ( $_ = ) { if ( /^\D+$/ ) { print $_; } I see 5 dogs I see five dogs./nonum.pl testfile I see five dogs

12 Regexp quantfier #!/usr/bin/perl while ( $_ = ) { if ( /a{2,4}/ ) { # look for aa, aaa, aaaa $var1 = $'; # store text after match chop $var1; # remove newline print "(", $&, " ", $`, " ", $var1, ")\n"; # (text matched text before match text after match) } else { if ( /a+/ ) { # other amounts of a’s print "Any number of A's: ",$_; }

13 cab ccaabb cccaaabbb ccccaaaabbbb cccccaaaaabbbbb ?????

14 cab ccaabb cccaaabbb ccccaaaabbbb cccccaaaaabbbbb Any number of A’s: cab

15 cab ccaabb cccaaabbb ccccaaaabbbb cccccaaaaabbbbb Any number of A’s: cab (aa cc bb)

16 cab ccaabb cccaaabbb ccccaaaabbbb cccccaaaaabbbbb Any number of A’s: cab (aa cc bb) (aaa ccc bbb)

17 cab ccaabb cccaaabbb ccccaaaabbbb cccccaaaaabbbbb Any number of A’s: cab (aa cc bb) (aaa ccc bbb) (aaaa cccc bbbb)

18 cab ccaabb cccaaabbb ccccaaaabbbb cccccaaaaabbbbb Any number of A’s: cab (aa cc bb) (aaa ccc bbb) (aaaa cccc bbbb) (aaaa ccccc abbbbb)

19 After-Match Special Variables $` $& $' Text before match Text matched Text after match

20 Capturing After a successful match, Perl provides variables $1, $2, $3 … which hold the text matched by their respective ( parenthesized subexpressions ) in the regex. –Subexpressions are numbered by counting open parentheses from the left starting at 1

21 #!/usr/bin/perl while ( $_ = ) { if ( /(c{1,3})(a{2,4})/ ) { print $2, " ", $1, "\n"; }

22 cab ccaabb cccaaabbb ccccaaaabbbb cccccaaaaabbbbb ?????

23 cab ccaabb cccaaabbb ccccaaaabbbb cccccaaaaabbbbb aa cc aaa ccc aaaa ccc

24 Command Line Arguments #!/usr/bin/perl # Extract the first argument from the command line # and place it into the variable $pattern. All # other arguments are treated as filenames. $pattern = while ( ) { if ( /$pattern/ ) { print $ARGV,": ",$_; }

25 Looking for a String in a File File1 SetSize ResetSize SETSIZE resetSIZE File2 This is a new file and its Size is very small../pgm2.pl Size File* ???????????

26 Looking for a String in a File File1 SetSize ResetSize SETSIZE resetSIZE File2 This is a new file and its Size is very small../pgm2.pl Size File* File1: SetSize File1: ResetSize File2: and its Size is very small

27 Looking for a String in a File File1 SetSize ResetSize SETSIZE resetSIZE File2 This is a new file and its Size is very small../pgm2.pl “is.* small” File* ???????????

28 Looking for a String in a File File1 SetSize ResetSize SETSIZE resetSIZE File2 This is a new file and its Size is very small../pgm2.pl “is.* small” File* File2: and its Size is very small.

29 #!/usr/bin/perl print "Do you want Greenwich Time (yes or no)?: "; # Get the input from STDIN $answer = ; chop $answer; if ( $answer eq "yes" ) { $seconds = = gmtime($seconds); print "Time: ",$datetime[5]," ",$datetime[4]," ",$datetime[3]," ",$datetime[2]," ",$datetime[1]," ",$datetime[0],"\n"; } else { $seconds = = localtime($seconds); print "Time: ",$datetime[5]," ",$datetime[4]," ",$datetime[3]," ",$datetime[2]," ",$datetime[1]," ",$datetime[0],"\n"; }

30 # Check to see if there are any command line arguments. # The ARGV special variable contains all the command line # arguments after the program name. print "The number of command line arguments: ",$#ARGV+1,"\n"; # $#ARGV returns the index number of the last item on the # command line, i.e. if the command line was: # pgm.pl one two three # then $#ARGV would be set to 2.

31 $ i = 0; if ( $#ARGV > -1 ) { while ( $i <= $#ARGV ) { if ( -e $ARGV[$i] ) { if ( -d $ARGV[$i] ) { print $ARGV[$i]," is a directory.\n"; } elsif ( -x $ARGV[$i] ) { print $ARGV[$i]," is an executable file.\n"; } elsif ( -T $ARGV[$i] ) { print $ARGV[$i]," is a text file.\n"; } else { print $ARGV[$i]," is not a file.\n"; } $i++; }