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

Similar documents
Scripting Languages Perl Basics. Course: Hebrew University

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

Pathologically Eclectic Rubbish Lister

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

What is PERL?

Scripting Languages. Diana Trandabăț

PERL Bioinformatics. Nicholas E. Navin, Ph.D. Department of Genetics Department of Bioinformatics. TA: Dr. Yong Wang

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

Perl. Interview Questions and Answers

1. Introduction. 2. Scalar Data

Programming introduction part I:

Lecture 2: Programming in Perl: Introduction 1

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

Examples of Using the ARGV Array. Loop Control Operators. Next Operator. Last Operator. Perl has three loop control operators.

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

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

9.1 Origins and Uses of Perl

Introductory Perl. What is Perl?

Arrays (Lists) # or, = ("first string", "2nd string", 123);

(Refer Slide Time: 01:12)

COMS 3101 Programming Languages: Perl. Lecture 2

Regular expressions and case insensitivity

Regular expressions and case insensitivity

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

CSC Web Programming. Introduction to JavaScript

The Power of Perl. Perl. Perl. Change all gopher to World Wide Web in a single command


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

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

PERL Scripting - Course Contents

Title:[ Variables Comparison Operators If Else Statements ]

DEPARTMENT OF MATHS, MJ COLLEGE

Course Outline. Introduction to java

COMS 3101 Programming Languages: Perl. Lecture 1

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

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

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

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

VLC : Language Reference Manual

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Computer Programming : C++

CMSC 331 Final Exam Fall 2013

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

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

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

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

Modularity and Reusability I. Functions and code reuse

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

Perl Programming. Bioinformatics Perl Programming

Beginning Perl. Mark Senn. September 11, 2007

Language Reference Manual

Computational Theory MAT542 (Computational Methods in Genomics) - Part 2 & 3 -

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

Full file at C How to Program, 6/e Multiple Choice Test Bank

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

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

Data Types and Variables in C language

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

PHP. Interactive Web Systems

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Introduction to CFX. Appendix A. Scripting and Automation A-1. ANSYS, Inc. Proprietary 2009 ANSYS, Inc. All rights reserved.

15.1 Origins and Uses of Ruby

More Perl. CS174 Chris Pollett Oct 25, 2006.

CS1 Lecture 3 Jan. 18, 2019

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

CSC105, Introduction to Computer Science I. Introduction. Perl Directions NOTE : It is also a good idea to

CSCI-GA Scripting Languages

Perl. Perl. Perl. Which Perl

Python Unit

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

JME Language Reference Manual

Lecture 1. Torgeir R. Hvidsten Assistant professor in Bioinformatics Umeå Plant Science Center (UPSC) Computational Life Science Centre (CLiC)

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Strings. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

YOLOP Language Reference Manual

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

CSCI-GA Scripting Languages

Chapter 2. Lexical Elements & Operators

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Tieing and Overloading Objects in Perl. Dave Cross Magnum Solutions

Introduction to Python

Introduc)on to Unix and Perl programming

Sequence Analysis with Perl. Unix, Perl and BioPerl. Why Perl? Objectives. A first Perl program. Perl Input/Output. II: Sequence Analysis with Perl

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

JavaScript I Language Basics

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Introduction to Python

Unix, Perl and BioPerl

CGS 3066: Spring 2015 JavaScript Reference

Introduction to C Final Review Chapters 1-6 & 13

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Variable and Data Type I

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Programming Languages and Uses in Bioinformatics

Full file at

Transcription:

Welcome to Research Computing Services training week! November 14-17, 2011 Monday intro to Perl, Python and R Tuesday learn to use Titan Wednesday GPU, MPI and profiling Thursday about RCS and services

Programming with Perl Katerina Michalickova The Research Computing Services Group SUF/USIT November 14, 2011

Perl (from perltutorial.org) Perl stands for Practical Extraction and Reporting Language. Perl is a general-purpose, interpreted programming language with a vast number of uses. Perl was invented by Larry Wall, a linguist working as a systems administrator at NASA in 1987. From the beginning, Perl was used to help processing reports faster and easier. Perl is very good and optimized for problems that handle 90% of text and 10% of other. Perl is best for short and small programs that can be entered and run on a single command line.

Topics Variables and operators Control structures Functions Input and output Regular expressions Hints and resources

What is a variable? Variable is a place to store your data $myvariable It is a label that represents your data and you can recall its value during execution of your program You can change the value of the variable (hence it is a variable) $myvariable = 3;..it is 3 $myvariable = $myvariable + 2;..it is 5 $myvariable = 1;..it is 1

Variables in Perl Scalars numbers and strings Arrays lists Hashes associative lists (Objects) b$a

$.. Scalar variables - numbers Integers 1,2,3 Floats 1.1, 1.2 Non-decimal 0xff, o377,ob11111111 $mynumber = 2; $mynumber = 5*6; $mynumber = $a + $b;

Numerical mathematical operators + Addition $a + $b - Subtraction $a - $b * Multiplication $a * $b / Division $a/$b % Modulus $a%$b ** Exponentiation $a**$b ( ) Grouping ($a+$b)*$c ++ Increment $a++ -- Decrement $b--

$.. Scalar variables - strings Any set of characters enclosed in quotes $mystring= I like summer. ; $mystring= 3or5_nonsense ;!Single or double quotes make a difference.

Special characters for strings \n..newline one line\nsecond line is one line second line \t..tab one\ttwo is one two BUT one line\nsecond line is one line\nsecond line

Strings operators Concatenation $a = honey ; $b = bee ; $a.$b is honeybee Length - length($c) is 8 Substring - substr($c,5,3) is bee Index - index($c, b ) is 5

@.. Arrays Arrays are ordered lists - elements within arrays can be scalars a b c d e 0 1 2 3 4 @myarray = ( a, b, c, d, e ); $myarray*1+ = b ;!Note: when accessing a single element use a $ sign.

Array operators qw easy way to declare array @myarray = qw(a b c d e); scalar length of an array scalar(@myarray); returns 5 pop returns the last element of array pop(@myarray); returns e and @myarray is now ( a, b, c, d ) push adds an element on the end of the array push(@myarray, f ); @myarray is now ( a, b, c, d, f ) shift returns the first element of the array shift(@myarray); returns a and @myarray is now ( b, c, d, f ) unshift adds an element to the beginning of the array unshift (@myarray, z ); @myarray is now ( z, b, c, d, f ) reverse reverse order of elements reverse(@myarray); @myarray is now ( f, d, c, b, z ) sort sort elements of the array sort(@myarray); @myarray is now ( b, c, d, f, z )

%.. Hashes Hash is collection of pairs of keys and values. Keys are unique strings that are used to index the hash. a b c d e ant bee centipede donkey elephant key.. ant and value.. a %myhash = ( ant => a, bee => b, centipede => c, donkey => d, elephant => e ); $myhash, bee - returns b $myhash, elephant - returns e

Hash functions keys keys(%myhash) returns ( ant, bee, centipede, donkey, elephant ) values values(%myhash) returns ( a, b, c, d, e ) exists exists $myhash, ant - is true exists $myhash, sloth - is false delete delete $myhash, elephant - removes the pair elephant => e from the hash

Objects you create an object to match you requirements objects have attached variables and methods objects can be abstracts or more specific if you have need for objects, consider other programming language german sheppard police dog dog schnauzer daschund

Control structures determine the order of operations within a program if($tired) no $water = yes ; yes if($very_tired) no $small_coffe= yes ; yes $large_coffe= yes ;

True or false? To make decision, the program evaluates a conditional expression $a = 4; $b = 5; $a == $b is false $a++; $a == $b is true

Comparison operators comparison numeric string equal == eq not equal!= ne less than < lt greater than > gt less than or equal to <= le greater than or equal to >= ge Make sure you use the right operators for numbers or strings,!using an incorrect one might have unpredictable results.

Types of control structures if makes one time decision while repeats part of the program until some condition is not true any more for repeats part of the program fixed number of times

if / elsif / else if ($bear_type eq black_bear ) { $climb_tree = no ; # black bears can climb trees } elsif ($bear_type eq grizzly ) { $climb_tree = yes ; } else { $climb_tree = no ; } # some grizzlies climb trees, though # it is most likely a polar bear # so there are no trees around

my $time = 9; while($time < 17) { $work = yes ; $time = current_hour(); } while! Bevare of infinite loops. while($time < 17) { $work = yes ; }

for for ($time = 9, $time < 17, $time++) { $work = yes ; }

Control structures for arrays and hashes foreach iterates through an array foreach $i (@myarray) { $sum = $sum + $i; } # sums up an array each iterates though a hash while (($key, $value) = each %myhash) { $sum = $sum + $value; } # sums up values in a hash

Functions/Subroutines separate logical units of a program make coding more manageable reusable each function contains a set of instructions that operate on pre-defined input and produce predefined output Perl contains hundreds of pre-defined functions readily available for use

Functions syntax sub array_max { my (@array) = @_; my $i = 0; my $max = $array[0]; foreach $i (@array) { if ($i > $max) { $max = $i; } } return $max; }

#!/usr/bin/perl use strict; use warnings; Functions syntax # this program select maximum from an array my @array = (2,4,3,6,8,9,1); my $mx = 0; $mx = array_max(@array);

Input and output programs usually read and produce data program can be interactive or can read data from a file files are read or written to using a special kind of variables filehandles named in CAPITAL LETTERS default for input is STDIN and output is STDOUT open, read, close operations

Interactive input print Please enter your name.\n ; $name = <STDIN>; chomp($name); print Please enter your age.\n ; $age = <>; chomp($age);

Reading a file open (IN, myfile.txt ); while(<in>) { $line = $_; chomp($line); } close (IN);

Writing to a file open(out, >myoutfile.txt ); open (IN, myfile.txt ); while(<in>) { $line = $_; chomp($line); $line = check_line($line); print OUT $line\n ; } close(in); close(out);

Regular expression Perl is powerful in manipulating text using regular expressions regular expressions are used to find matching patterns in text patterns can be made extremely general syntax of regular expressions can be studied at http://www.perl.com/doc/manual/html/pod/perlre.html the following examples are taken from http://www.cs.tut.fi/~jkorpela/perl/regexp.html

Simple matching my $greeting = "Hello World" ; if ($greeting =~ /Hello/) { print Hello found.\n"; } else { print Hello not found.\n"; }

Metacharacters ^ beginning of string $ end of string. any character except newline * match 0 or more times + match 1 or more times? match 0 or 1 times alternative ( ) grouping [ ] set of characters { } repetition modifier

Repetition a* zero or more a+ one or more a? zero or one a{m} exactly m a{m,} at least m a{m,n} at least m but at most n

Matching with \ \w matches any single character classified as a word character (alphanumeric or _ ) \W matches any non- word character \s matches any whitespace character (space, tab, newline) \S matches any non-whitespace character \d matches any digit character, equiv. to [0-9] \D matches any non-digit character \b word boundary \B not a word boundary

Examples abc ^abc abc$ a b ^abc abc$ ab{2,4}c ab{2,}c ab*c ab+c ab?c a.c a\.c abc (that exact character sequence, but anywhere in the string regular expressions are greedy) abc at the beginning of the string abc at the end of the string either of a and b the string abc at the beginning or at the end of the string an a followed by two, three or four b s followed by a c an a followed by at least two b s followed by a c an a followed by any number (zero or more) of b s followed by a c an a followed by one or more b s followed by a c an a followed by an optional b followed by a c (abc or ac) an a followed by any single character (but not a newline) followed by a c a.c exactly ( \ is an escape character)

More examples [abc] [Aa]bc [abc]+ [^abc]+ \d\d \w+ a\s*bc abc\b perl\b any one of a, b and c either of Abc and abc any (nonempty) string of a s, b s and c s (such as a, abba, acbabcacaa) any (nonempty) string which does not contain any of a, b and c (such as defg) any two decimal digits, such as 42; same as \d{2} a word, a nonempty sequence of alphanumeric characters (and underscores), such as foo and 12bar8 and foo_1 the strings a and bc optionally separated by any amount of white space (spaces, tabs, newlines) abc when followed by a word boundary (e.g. in abc! but not in abcd) perl when not followed by a word boundary (e.g. in perlert but not in perl stuff)

Substitution $string = This apple is mine, this orange is yours and this pear is his. ; s/this/that/ s/this/that/g s/this/that/gi This apple is mine, that orange is yours and this pear is his. This apple is mine, that orange is yours and that pear is his. That apple is mine, that orange is yours and that pear is his.

Split function #!/usr/bin/perl use strict; use warnings; my $data = Oslo,Blindern,IFI2,Prolog ; my @values = split(/,/, $data); foreach my $val (@values) { print "$val\n"; }

Hints use warnings use strict undef and defined before assigning a value or using my, a new variable has a status undef and Perl ignores it to test the status use defined($myvariable) be careful about a numerical versus a string context

Resources Perl documents http://perl.org or http://perldoc.perl.org 22 500 modules wealth of written code; search http://cpan.org this lecture was inspired by the Canadian bioinformatics workshops material, see the originals at http://donaldson.uio.no/wiki/mbv3070

Thank you my $text = It is time for Perl. ; if ($text =~ /Perl/) { $text =~ s/perl/a break/; print $text\n ; }