3.1 Revision: variables & arrays Array declaration Array = ('A','B','C','D'); = = (); Array.

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

Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
UNIT 12 UNIX I/O Redirection.
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.
Lecture 2 Introduction to C Programming
Introduction to C Programming
COMP234 Perl Printing Special Quotes File Handling.
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
4.1 Reading and writing files. 4.2 Open a file for reading, and link it to a filehandle: open(IN, "
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.
6a.1 More about files: Globbing. 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "
Where to get Perl ? / Unix/Linux/Solaris Unix/Linux/Solaris Interpreter (called “perl”) comes pre-installed usually Interpreter.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
Perl File I/O Learning Objectives: 1. To understand the usage of file handler in Perl 2. To learn the file error handling / testing in Perl.
5.1 Previously on... PERL course (let ’ s practice some more loops)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Perl File I/O Software Tools. Slide 2 Filehandles l A filehandle is the name for an I/O connection between your Perl program and the outside world. STDIN.
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.
4.1 Revision. 4.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
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.
Introduction to C Programming
3.1 Ifs and Loops. 3.2 Revision: variables Scalar variables can store scalar values: Variable declaration my ($priority); Numerical assignment $priority.
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);
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Prof. Alfred J Bird, Ph.D., NBCT -bird.wikispaces.umb.edu/ Office – McCormick 3rd floor.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
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
5 1 Data Files CGI/Perl Programming By Diane Zak.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
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.
Scripting Languages Course 3 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
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.
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)
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
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.
The Scripting Programming Language
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Input from STDIN STDIN, standard input, comes from the keyboard.
Part 1: Basic Commands/Utilities
Variables, Expressions, and IO
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Perl Variables: Array Web Programming.
Basic Input/Output 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.
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Presentation transcript:

3.1 Revision: variables & arrays Array declaration Array = ('A','B','C','D'); = = (); Array element: print $array[1]; B $array[0] = '*';*BCD Array size: print Empty array

3.2 Revision(+): Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10); ($a,$b) $a = ($a, = Multiple declarations $a='cow' $b='dog' $a=6; $b=7 will be useful for home assignment

3.3 Revision: arrays function Pop & my $x = A B 17 print $x; A B 17 hi Z Shift & my $x = B 17 hi print $x; A print * A B 17 Note: These functions change the array (argument) 0 "A" "B" 17"hi"

3.4 my $str = "So-long-and-thanks-for-all-the-fish"; = split(/-/, $str); $str = join("!! ); print "$str\n"; So!! Long!! and!! thanks!! for!! all!! the!! Fish = ("b","a","c"); = = Revision: arrays function (2) Note: These functions do NOT change the array (argument), But return the

3.5 Revision: reading input Reading a single line: my $line = <STDIN>; Reads a single line End input with Enter First line Reading a list of lines: = <STDIN>; Each line is a different element in an array Hit Ctrl-z to end input Note: in Mac use Ctrl-d. First line Second Last Ctrl-z $line "First "First line\n""Second\n""Last\n" 0 1 2

3.6 Reading and writing files

3.7 Open a file for reading, and link it to a filehandle: open(IN, "<input.txt"); And then read lines from the filehandle, exactly like you would from : my $line = ; Read a single line from file to scalar (and move to the next line) = ; Read all file to array Every filehandle opened should be closed: close(IN); Always check the open didn ’ t fail (e.g. if a file by that name doesn ’ t exists): open(IN, "<$file") or die "can't open file $file"; Reading files

3.8 Open a file for writing, and link it to a filehandle: open(OUT, ">output.txt") or die... NOTE: If a file by that name already exists it will be overwriten! You could append lines to the end of an existing file: open(OUT, ">>output.txt") or die.. Print to a file (in both cases): print OUT "This is an output line.\n"; Writing to files no comma here

3.9 You can ask questions about a file or a directory name (not filehandle): if (-e $name) { print "The file $name exists!\n"; } -e$name exists -r$name is readable -w$name is writable by you -z$name has zero size -s$name has non-zero size (returns size) -f$name is a file -d$name is a directory -l$name is a symbolic link -T$name is a text file -B$name is a binary file (opposite of -T). File Test Operators

3.10 open( IN, '<D:\workspace\Perl\p53.fasta' ); Always use a full path name, it is safer and clearer to read Remember to use \\ in double quotes open( IN, "<D:\\workspace\\Perl\\$name.fasta" ); (usually) you can also use / open( IN, "<D:/workspace/Perl/$name.fasta" ); Working with paths

3.11 Working with files - example use strict; = ; = © th Century Fox - All Rights Reserved

3.12 use strict; my $inFile = 'D:\fight club.txt'; open(IN, "<$inFile") or die "cannot open $inFile"; = ; = close(IN); Working with files - example © th Century Fox - All Rights Reserved

3.13 use strict; my ($inFile,$outFile) = ('D:\fight club.txt','D:\sorted.txt'); open(IN, "<$inFile") or die "cannot open $inFile"; open(OUT, ">$outFile") or die "cannot open $outFile file"; = ; = print close(IN); close(OUT); Working with files - example © th Century Fox - All Rights Reserved

3.14 Class exercise 3a Write scripts that perform as follows: 1.Read the first and the second line of the "fight club.txt" file, and print them to the screen. 2.Read the entire "fight club.txt" file and print all the file in one line to an output file names "fight.one.txt". 3.The file "numbers.txt" contains numbers, one in each line. Read the first 3 numbers in the files and print their sum to a file called "sum.txt". 4*.Print only the 3 rd and the 5 th lines of the file "fight club.txt".

- revision 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 "Your arguments are:\n"; print "fight club"2

5.16 It is common to give arguments (separated by spaces) within the command-line for a program or a script: They will be stored in the : my $inFile = $ARGV[0]; my $outFile = $ARGV[1]; Or more simply: my ($inFile,$outFile) Command line arguments > perl -w findProtein.pl D:\perl_ex\in.fasta D:\perl_ex\out.txt

3.17 use strict; my ($inFile,$outFile) = ('D:\fight club.txt','D:\sorted.txt'); open(IN, "<$inFile") or die "cannot open $inFile"; open(OUT, ">$outFile") or die "cannot open $outFile file"; = ; = print close(IN); close(OUT); Working with files - example © th Century Fox - All Rights Reserved

3.18 use strict; my ($inFile,$outFile) open(IN, "<$inFile") or die "cannot open $inFile"; open(OUT, ">$outFile") or die "cannot open $outFile file"; = ; = print close(IN); close(OUT); Working with files - example © th Century Fox - All Rights Reserved

3.19 Class exercise 3b 1.Repeat question 3a.1: Read the first and the second line of the "fight club.txt" file, and print them to the screen. This time receive the input file 2.Repeat question 3a.2: Read the entire "fight club.txt" file and print all the file in one line to an output file names "fight.one.txt". This time receive both the input and output file