Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

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.
The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
● Perl reference
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
Programming and Perl for Bioinformatics Part III.
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.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Lecture 2 BNFO 240. Perl online references
5.1 Previously on... PERL course (let ’ s practice some more loops)
Subroutines sub { –#parameters are placed – –. –return; }
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
Lecture 4 BNFO 235 Usman Roshan. IUPAC Nucleic Acid symbols.
BNFO 235 Lecture 5 Usman Roshan. What we have done to date Basic Perl –Data types: numbers, strings, arrays, and hashes –Control structures: If-else,
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.
I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
Perl - Advanced More Advanced Perl  Functions  Control Structures  Filehandles  Process Management.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Introduction to Perl Thaddeus Aid IT Learning Programme University of Oxford 15/04/2014.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Lecture 8 perl pattern matching features
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
If statements while loop for loop
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Sed, awk, & perl CS 2204 Class meeting 13 *Notes by Mir Farooq Ali and other members of the CS faculty at Virginia Tech. Copyright 2003.
Time to talk about your class projects!. Shell Scripting Awk (lecture 2)
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
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,
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Lecture 2 BNFO 601.
Introduction to Python
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Perl Variables: Array Web Programming.
Introduction to Python
Introduction to Perl Jarrad Battaglia.
Pemrosesan Teks Herika Hayurani Sept 19, 2006
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Python Basics with Jupyter Notebook
Introduction to Computer Science
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Challenge Guide Grade Code Type Slides
Presentation transcript:

Lecture 2 BNFO 135 Usman Roshan

Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Number operators

Strings Perl supports very powerful operations on strings. Basic operators below.

Arrays Array variables begin Example “rna”, “protein”); outputs dnarnaprotein

Array operations

Hashes Hashes are like arrays, except that they are indexed by user defined keys instead of non-negative integers. Hashes begin with % Examples –%h=(‘red’, 1, ‘blue’, 2, ‘green’, 3); –%h=(“first”, “usman”, “last”, “name”);

Perl online references

Input and Output printf: C-like formatted output, very powerful print: unformatted output Example: –printf “My name is %s\n”, $name; –printf “Today is %s %d %d\n”, $month, $day, $year;

File I/O open(IN, “in.txt”); $line= ; print $line; close IN; open(OUT, “>out.txt”); print OUT “line1\n”; close OUT;

File I/O open(OUT, “>>out.txt”); print OUT “line2\n”; close OUT; open(IN, ; #reads all lines into array close IN; $input= ; #read from standard input

Control structures--- if else block if (c) { s1; s2 ;...;} if (c) { s1; s2;...;} else { s1; s2;...;} if (c1) { s1; s2;...;} elsif (c2) {... } elsif (c3) {....} else {...};

Control structures--- if else block This is program to compute max of two Numbers. $max=0; if($x > $y){ $max = $x;} else {$max = $y;}

Control structures--- for loop for(init_exp; test_exp; iterate_exp) –{s1; s2; … ;} foreach $i { s1; s2;...; } Examples: for ($i=0; $i $i++) –{ print $arr[$i]; } foreach $e –{ print $e; }

Control structures--- while loop while (condition) { s1; s2;...; } do { s1; s2;...;} while condition; while (condition) {s1;if(c){last;} s2;...; } #last means exit the loop while (condition) {s1;if(c){next;} s2;...; } #next means go to next iteration

Write a program to prompt the user for a Yes or No response. Read in the user response using the STDIN file handle and print “OK” is the user enters “Yes,” “I hear you” if the user enters “No,” and “Make up your mind!” if the user enters something other than “Yes” or “No.” Create a script called foods.pl that asks the user for their favorite foods. Ask the user to enter at least 5 foods, each separated by a space (or some other delimiter). Store their answer in a scalar. Split the scalar into an array. Once the array is created, have the script do the following: * Print the array * Print the number of elements in the array * Create an array slice from three elements of the array and print the new array Write a program to open a file containing SSNs and read in the first five records into an array.

Write a program called count.pl that reads in a set of strings from a file called string.txt and prints the number of A’s, B’s, C’s, and D’s. For example, if the file looks like AABCAA CDAAD CCC then your program should output A6 B1 C5 D2

Subroutines sub { –#parameters are placed – –. –return; }

Scope You can create local variables using my. Otherwise all variables are assumed global, i.e. they can be accessed and modified by any part of the code. Local variables are only accessible in the scope of the code.

Pass by value and pass by reference All variables are passed by value to subroutines. This means the original variable lying outside the subroutine scope does not get changed. You can pass arrays by reference using \. This is the same as passing the memory location of the array.

Splitting and joining strings split: splits a string by regular expression and returns array = split(/,/); = split(/\s+/); join: joins elements of array and returns a string (opposite of split)

Searching and substitution $x =~ /$y/ ---- true if expression $y found in $x $x =~ /ATG/ --- true if open reading frame ATG found in $x $x !~ /GC/ --- true if GC not found in $x $x =~ s/T/U/g --- replace all T’s with U’s $x =~ s/g/G/g --- convert all lower case g to upper case G

DNA regular expressions Taken from Jagota’s Perl for Bioinformatics