2.1 Lesson 2: Scalar Functions and Arrays “Perl programming is an empirical science!” - Larry Wall.

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

Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is.
COMP234 Perl Printing Special Quotes File Handling.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Practical Extraction & Report Language Picture taken from
1.1 Perl Programming for Biology The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman and Dudu.
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
1ex.1 Perl Programming for Biology Exercise 1 The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
1.1 Perl Programming for Biology G.S. Wise Faculty of Life Science Tel Aviv University, Israel October 2009 David Burstein and Ofir Cohen.
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
PHP : Hypertext Preprocessor
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
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.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
COMP519: Web Programming Autumn 2010 Perl Tutorial: The very beginning  A basic Perl Program The first line Comments and statements Simple printing 
Introduction to Perl Yupu Liang cbio at MSKCC
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
3.1 Revision: variables & arrays Array declaration Array = ('A','B','C','D'); = = (); Array.
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
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
1.1 Perl Programming for Biology G.S. Wise Faculty of Life Science Tel Aviv University, Israel October 2012 Eli Levy Karin and Haim Ashkenazy
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
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
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Variables, Expressions, and IO
Perl Variables: Array Web Programming.
Introduction to C++ Programming
Teaching London Computing
Context.
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
T. Jumana Abu Shmais – AOU - Riyadh
Perl I/O Learning Objectives:
Introduction to Perl Learning Objectives:
CIS 136 Building Mobile Apps
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Presentation transcript:

2.1 Lesson 2: Scalar Functions and Arrays “Perl programming is an empirical science!” - Larry Wall

2.2 Your very first Perl script print "Hello world!"; A Perl statement must end with a semicolon “ ; ” The print function outputs some information to the terminal screen Now – do it yourself: Write this script in notepad Start  Accessories  Notepad And save (file  save) your script in D:\ex_perl (my computer  D:  perl_ex) With the name hello.pl

2.3 Your very first Perl script print "Hello world!"; Traditionally, Perl scripts are run from a command line interface Start it by clicking: Start  Accessories  Command Prompt or: Start  Run…  cmd

2.4 Your very first Perl script print "Hello world!"; First let’s go to the correct directory: D: - change drive from C: to D: cd perl_ex - change directory to perl_ex dir - list all the files in the directory (you should see your scirpt here) Running a Perl script perl –w SCRIPT_NAME

2.5 Common DOS commands: d: change to other drive (d in this case) md my_dir make a new directory cd my_dir change directory cd.. move one directory up dir list files (dir /p to view it page by page) help list all dos commands help dir get help on a dos command (hopefully) auto-complete go to previous/next command -c Emergency exit More tips about the command line are founds here.here Running Perl at the Command Line

2.6 Your very first Perl script print "Hello world!"; Now – change it to your own name… print something additional. And run it again…

2.7 Your very first Perl script print "Hello world!"; Compare this to Java's "Hello world": public class HelloWorld { public static void main(String[] args) { System.out.print("Hello World!"); }

2.8 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division) ** (exponentiation) ++ (auto-increment) -- (auto-decrement) string double quote: print "hello\tworld"; helloworld single quote (as is): print 'hello\tworld'; hello\tworld operators:. (concatenate) x (replicate)‏

2.9 Variables - revision Always use use strict; Variable declaration my $priority; String assignment $priority = 'high'; Numerical assignment $priority = 1; Copy variable my $max = $priority; Change variable $max = $max + 1; $max++;

2.10 $a$b my $a = 1; my $b = $a; $b = $b+1; $b++; $a--; Variables For example:

2.11 Scalar Data and functions

2.12 Variables - notes and tips Tips: Give meaningful names to variables: e.g. $studentName is better than $n Always use an explicit declaration of the variables using the my function Note: Variable names in Perl are case-sensitive. This means that the following variables are different (i.e. they refer to different values): $varname = 1; $VarName = 2; $VARNAME = 3;

2.13 Variables - always use strict! Always include the line: use strict; as the first line of every script. “Strict” mode forces you to declare all variables by my. This will help you avoid very annoying bugs, such as spelling mistakes in the names of variables. my $varname = 1; $varName++; Warning: Global symbol "$varName" requires explicit package name at... line...

2.14 Interpolating variables into strings use strict; my $a = 9.5; print "a is $a!\n"; a is 9.5! Reminder: print 'a is $a!\n'; a is $a!\n

2.15 Uninitialized variables Uninitialized variable (before assignment) recieves a special value: undef If uninitialized variables are used a warning is issued: my $a; print($a+3); Use of uninitialized value in addition (+) 3 print("a is :$a:"); Use of uninitialized value in concatenation (.) or string a is ::

2.16 Reading input <STDIN> allows us to get input from the user: use strict; print "What is your name?\n"; my $name = <STDIN>; print "Hello $name!"; What is your name? Shmulik Hello Shmulik ! $name: "Shmulik\n"

2.17 $name: "Shmulik\n" Use the chomp function to remove the “new-line” from the end of the string (if there is any): use strict; print "What is your name?\n"; my $name = <STDIN>; chomp $name; # Remove the new-line print "Hello $name!"; What is your name? Shmulik Hello Shmulik! $name: "Shmulik"$name: Reading input - chomp

2.18 The length function The length function returns the length of a string: my $str = "hi you"; print length($str); 6 Actually print is also a function so you could write: print(length($str)); 6

2.19 The substr function The substr function extracts a substring out of a string. It receives 3 arguments: substr(EXPR,OFFSET,LENGTH) Note: OFFSET count start from 0. For example: my $str = "university"; my $sub = substr($str, 3, 5); $sub is now "versi", and $str remains unchanged. Also note : You can use variables as the offset and length parameters. The substr function can do a lot more, Google it and you will see…

2.20 Documentation of perl functions Anothr good place to start is the list of All basic Perl functions in the Perl documentation site: Click the link “Functions” on the left (let's try it…)

2.21 Class exercise Write a script that print to the screen the value of 2 in the power of 100 (2 100 ). 2.Write a script that reads a line from the user (using STDIN) and prints the length of it. 3.Write a script that read a line from the user and print the string from the 5 rd letter to the 7 th one. For example for the input: “ The Simpsons ” The script will output: “ Sim ” Reminder: The position of the 1 st letter is 0 (zero).

2.22 Lists and Arrays

3 2 * 2 1"fred" Lists and arrays A list is an ordered set of scalar values: (3,2,1,"fred") An array is a variable that holds a list: = (3,2,1,"fred"); You can access an individual array element: print $arr[1];2 $arr[0] = "*"; 3 2 1"fred"

2.24 Lists and arrays You can easily get a sub-array: = (3,2,1,"fred","bob"); 321fredbob print $arr[1]; 2 You can extend an array as much as you like: = ("A","B","C") $arr2[5] = 3 2 1"fred" "bob" "fred" 0 "A""A""B""B" "F""F" undef "A""A""B""B" "C""C"

2.25 Lists and arrays Assigning to arrays: = ('alpha','bravo','charlie','delta'); Multiple assignment: my ($a,$b) = ('cow','dog'); $a='cow'; $b='dog'; my = (1..5);$a=1; scalar - counting array elements: print 4 Last element index: (might issue warning) print $#arr; 3 Last element value: print $arr[-1]; delta

2.26 Interpolating arrays You can interpolate arrays and array elements into strings: = ("a","b","cat",d"); abcatd print a b cat d print "$arr[2] is the third element of cat is the third element

2.27 Reading arrays You can read lines from the standard input in list context: = ; will store all the lines entered until the user hits ctrl-z. ctrl-z to indicate end of "cogito\n""ergo\n""sum\n"

2.28 Reading arrays chomp will work on each entry of the array = ; "cogito""ergo""sum"

2.29 Manipulating arrays – push & pop = ('a','b','c','d','e'); = ('a','b','c','d','e'); my $num = print $num;e e f a b c d $num

2.30 shift & unshift = ('a','b','c'); my $num = print $num;a = ('a','b','c');

2.31 split & join my = split(/ /, "You talkin to me? You talkin to = = split(//, "You talkin to me? You talkin to = ("Y","o","u"," ","t","a","l","k","i","n"," ",...) my $str = print "$str\n"; "You-talkin-to-me?-You-talkin-to-me?"

2.32 Reversing lists = ("yossi","bracha","moshe"); = print moshe;bracha;yossi

2.33 Sorting lists Default sorting is alphabetical: = sort("yossi","bracha","moshe"); is ("bracha","moshe","yossi") = sort(1,3,9,81,243); is (1,243,3,81,9) Other forms of sorting require subroutine definition: = sort(compare_sub 1,3,9,81,243); We might get to that latter… Note: sort and reverse do not change the array. They return a new array after the operation of the function (that should be saved in another array if needed for further use).

2.34 Class exercise Read a number from the first line of input, and then read the rest of the lines into an array (stop by using ctrl-z). Print the line selected by the number provided in the first line. 2.Read a list of numbers separated by spaces, and print those numbers in reverse order, separated by slashes (/). 3.Read a list of words separated by spaces, sort and print them. 4.Like in 3, but return only the last word (after the sorting). 5.Like in 3, but reverse the order of the letters of the last word

2.35 Comments on exercise 1 Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not produce any warnings The file should end with.pl extension. Write a separate file for each question, and name the scripts: “ ex2.1.pl ”, “ ex2.2.pl ”, “ ex2.3.pl ”… When submitting exercises by write your name and the exercise number in the subject line (for example “ Shmulik Israeli perl ex. 2 ”) You can use the computer classroom if you wish.

2.36 Comments on exercises Use meaningful names for variables. We always add " use strict; " in the first line of scripts (once is enogh). Always declare variables using " my $newVarName; ". Use chomp function to omit newline ( "\n" ) from input.

2.37 Comments on exercises Mind the input (parameters given by user) and the arguments (parameters used by functions): The input of Question 4 is somewhat tricky… The parameters of substr are: 1) string 2) start 3) LENGTH and in the exercise the request was that the numbers will be 1) start 2) stop 3) duplications

2.38 Adding comments Comments: The # symbol, and anything from it to the end of the line is ignored. # read two values from the user my $num1 = my $num2 = # calculate their sum my $sun = $num1 + $num2;

2.39 Adding comments Comments: If you want to insert a comment of multiple lines, you can use =begin and =cut. =begin This program prints stuff. Here you can write any text you want and you don’t need any # =cut print "stuff\n";