2.1 Scalar data - revision numeric 3 -20 3.14152965 6.35e-14 ( = 6.35 × 10 -14 )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)

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

Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
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.
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.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
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
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
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.
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.
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.
13r.1 Revision (Q&A). 13r.2 $scalar 13r.3 Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10);
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
PHP : Hypertext Preprocessor
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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.
– Introduction to Perl 10/20/ Introduction to Perl - Lists and Arrays Introduction to Perl Session 3 · lists and arrays.
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 
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
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.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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
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.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
2.1 Lesson 2: Scalar Functions and Arrays “Perl programming is an empirical science!” - Larry Wall.
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.
CSC 4630 Perl 2 adapted from R. E. Beck. I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Variables, Expressions, and IO
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Perl Variables: Array Web Programming.
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:
Python Strings.
CIS 136 Building Mobile Apps
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Presentation transcript:

2.1 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.2 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++; Interpolating in string my $str = "max is $max"; print variable print "$str\n"; print $str."\n"; Notes:  Variable names in Perl are case-sensitive  Give meaningful names to variables

2.3 Input & functions - revision Read line from user my $line = ; Remove "\n" (if exists) chomp $line; Length in characters length($line); Substring substr(EXPR,OFFSET,LENGTH) Note that OFFSET starts from 0. Example: my $string = "So Long, and Thanks for All the Fish"; my $sub = substr($string,1,4); print $sub; o Lo

2.4 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not produce any warnings When you us problems you run into, send the script, and copy the running of it (with “ perl -w ”) and the output from the command prompt window When submitting exercises by write your name and the exercise number in the subject line (for example “ Shmulik Israeli perl ex. 2 ”) Write a separate file for each question, and name the scripts: “ ex2.1.pl ”, “ ex2.2.pl ”, “ ex2.3.pl ”…

2.5 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.6 Comments on exercises Mind the input (parameters given by user) and the arguments (parameters used by functions): The last question (Q5) was 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 So you should use the STOP provided by the user and manipulate it to get the LENGTH that the substr function uses: (LENGTH = STOP – START + 1)

2.7 Adding comments Comments: The # symbol, and anything from it to the end of the line is ignored. # get start and stop values from the user my $start = my $stop = # calculate string length my $length = $start - $stop + 1;

2.8 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";

2.9 Uninitialized variables If uninitialized variables are used (before assignment) 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.10 Lesson 2: 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.12 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.13 Lists and arrays Assigning to arrays: = (3..6);# same 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: print $#arr; 3

2.14 Arrays in scalar context Consider the following: = ('a','b','c','d'); my $num = $#arr; $num = 3 my $num $num = 4 my ($num1, $second) $num1 = 'a' $second = 'b' This one will be very useful in the future

2.15 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.16 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.17 Reading arrays chomp will work on entrie arrays: = ; "cogito""ergo""sum"

2.18 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.19 shift & unshift = ('a','b','c'); my $num = print $num;a = ('a','b','c');

2.20 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.21 Reversing lists = ("yossi","bracha","moshe"); = print moshe;bracha;yossi

2.22 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.23 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

It is possible to pass arguments to Perl from the command line. These Command-line arguments are stored in an array created automatically : use strict; my $joinedArr = print "Hi" "man" 5

It is possible to pass arguments to Perl from the command line. These Command-line arguments are stored in an array created automatically : use strict; my $joinedArr = print "Hi man"5

2.26 Class exercise Read a list of numbers from the command-line and print those numbers in reverse order, separated by slashes (/). 2.Read a list of words from the command-line, sort and print them. 3.Like in 2, but return only the last word (after the sorting). 4.Like in 2, but reverse the order of the letters of the last word