Lecture 2 BNFO 240. Perl online references

Slides:



Advertisements
Similar presentations
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
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.
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Programming and Perl for Bioinformatics Part III.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
Lecture 4 BNFO 235 Usman Roshan. IUPAC Nucleic Acid symbols.
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;
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Programming for Linguists An Introduction to Python 24/11/2011.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Lists in Python.
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 9 More About Strings.
Chapter 2 - Algorithms and Design
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
If statements while loop for loop
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (2) Ruibin Bai (Room AB326) Division of Computer Science The University.
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.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
I Power Higher Computing Software Development High Level Language Constructs.
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,
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
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.
GE3M25: Computer Programming for Biologists Python, Class 5
Perl Day 5. Arrays vs Hash Arrays are one way to store multiple things in a variable. Hashes are another. Arrays are one way to store multiple things.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
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.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
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)
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
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.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
String and Lists Dr. José M. Reyes Álamo.
Lecture 2 BNFO 601.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Perl Variables: Array Web Programming.
Introduction to Python
© Akhilesh Bajaj, All rights reserved.
Introduction to TouchDevelop
String and Lists Dr. José M. Reyes Álamo.
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Introduction to Computer Science
Presentation transcript:

Lecture 2 BNFO 240

Perl online references

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”);

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

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

Exercises Write a program to read in two DNA sequences and print the number of matches and mismatches. Write a program to translate a DNA string into amino acids Write a program that searches for the open reading frame in a DNA sequence.

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 Soft copy due by Monday midnight in format FirstnameLastnamecount.pl ed to and hard copy due in a box outside my office by Tuesday

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.

A. Input your name into a variable called $name and then print "Hello, (your name here)". B. Input your age into a variable called $age. Have the computer print out how old you'll be eight years from now, ten years from now, twenty-four years from now. C. Input a word to a variable. Then store the first two letters of that word into another variable and print those two letters to the screen. D. Use the length function to find the length of a string. Then print the last half of the string to the screen. E. Input your firstname and lastname as two separate variables called $firstname and $lastname respectively. Concatenate them together using the dot operator '.' into a new variable called $wholename. Then print out $wholename.

A. Modify if1.pl to get an inputted number for $x. print "Enter a number: "; B.Fix if2.pl to actually get the else to happen. C. Write a program that adds two numbers and then prints out whether the sum of those two numbers is positive or negative. D. Input a number and put it into your program. Have a friend attempt to guess the number. Have your program tell your friend whether their guess was high, low, or correct. E. Enter your age and your score on the permit test (out of 15). Have the computer output whether or not you're able to get your permit based on being at least 16 years old and having a score of at least 12.

A. Write a for() loop that counts from 0 to 15 by twos and prints out the number that its on. B. Write a while() loop that counts from 0 to 15 by threes and prints out the number that it is on. C. Guessing Game revisited: Alter your guessing game to repeat itself until the user guesses the correct number. Output the number of guesses that the person made. D. Make an endless loop. If you are successful, you will need to know that control+c is the keyboard command to kill the program. E. Print out a multiplication table from 0 to 9. (Hint, use nested loops.)

A. Write a program that asks a user for five groceries, and then store that list in an array Print out the items in the array alphabetically. B. Write a program that asks the user for some numbers. Store them in an array Print out the numbers in the opposite order that they were entered. Stop accepting input when the user enters a letter.

A. Write a program that asks the user for five groceries, and then write them to a file in alphabetical order. (Hint: This is similar to an array exercise.) B. Write a program that reads the file wrtitten in the last ecxercise, and then prints the contents of the file to the screen in the opposite order as they appeared in the file. C. Still working on the grocery list, ask the user for the name of a file that contains their grocery list. Then display the contents of that file, and ask the user if they wish to leave the list alone, add to the list, or replace the list. If the user wishes to add to or replace the list, take the input and write it to the file, as appropriate.

Exercise 3.1 Write a program to print out the cube root of a command line argument. Remember that taking the Nth root of a number is equivalent to raising it the the 1/N power. Exercise 3.2 Given a string and a number as command line arguments, write a program to print the string out as many times as the number indicates. (You don’t need a loop.) Exercise 3.3 Write a program to add up the size (in bytes) of all the text files whose names are entered on the command line. Print out the total.

Exercise 4.1 Prompt the user for a first name and last name, and read in both names at the same time into a single variable. Assume that the input will be in the correct format with precisely one space between the names. Use index to locate the space, and use substr to parse the name. Convert the last name to UPPERCASE and the first name to Title Case. Print the results in the format “LAST, First”. Exercise 4.2 Modify Exercise 4.1 to prompt for and read in the names inside of a loop. Open a file for writing and print each name, formatted as “LAST, First” to the output file. Continue looping until the user enters the null string in response to the prompt for a name. Exercise 4.3 Print out two lines in the following format (adjusting for today’s date): Today is "Tuesday, December 8, 1998" already. It's day 342 of the year. Use localtime in a list context to determine today’s date. Assign the names of the twelve months of the year to one array and the names of the seven days of the week to another. Exercise 4.4 (Optional) Using Perl’s srand and rand functions write a program to simulate a Pick 6 Lotto program. Assuming a range of numbers from 1 through 46, pick 6 numbers and store them in an array. Make sure the same number is not selected more than once. Once the array is filled with 6 random values display the 6 numbers.

Exercise 5.1 Use the range operator to fill an array with the numbers = (30..40); Compare what happens when you use the reverse operator in a list context versus a scalar = # list context $var = # scalar context Print out the values of array and the $var scalar. Exercise 5.2 Fill an array with the names of some animals commonly found in a zoo. Experiment with some of the list functions: 1. Delete one animal from the end of the array. Print out the name of the animal deleted. 2. Add two new animals to the beginning of the array. 3. Add one new animal to the end of the array. 4. Delete one animal from the beginning of the array and add it to the end of the array. Repeat this rotation two more times. 5. Use the grep function to select all the animal names with lengths greater than 5 characters. Assign to a new array and print it. 6. Use the map function to convert each animal name to uppercase. Assign the results back to the original array. 7. Print out the entire array by deleting one element at a time from the beginning of the array until the array is emptied.

Exercise 5.3 Write a program that captures the outputs of two different commands in two separate arrays. Produce a merged listing of their respective outputs. That is, print line #1 of the first program, then line #1 of the second, then line #2 of the first program, then line #2 of the second, etc. For Unix systems, try using the commands who and w. Exercise 5.4 (Optional) Modify Exercise 4.4 (Pick 6 Lotto) to sort the 6 random numbers numerically before displaying. Exercise 5.5 (Optional) Fill an array with the 26 letters of the alphabet, and print the letters in random order. (Hint: Try using splice with a random subscript into the array to extract and delete one letter at a time.)

Exercise 6.1 Write a program that prints out each string entered on the command line once only. For example, $ ex6.1 red green red blue green yellow red red green blue yellow Use a hash to keep track of which strings have already been seen. Exercise 6.2 Write a program to print out a word frequency count of the strings entered on the command line, sorted from most frequently to least frequently seen string. For example, $ ex6.2 red green red blue green yellow red 3 red 2 green 1 blue 1 yellow

Exercise 6.3 Write a program to create a reverse mapping of a hash. Starting with a %phone hash, such as: %phone = ( tom => ' ', larry => ' ', sriram => ' ', ); that uses names as keys and phone numbers as values, build a new hash that uses the phone numbers as keys and names as values. Print out the key–value pairs of the new hash. What will happen if two or more people in the %phone hash have the same phone number? Exercise 6.4 (Optional) Modify Exercise 4.4 (Pick 6 Lotto) to use a hash for keeping track of which random numbers have already been seen. Store the chosen numbers by using them as keys in the hash. Although some value must be assigned for each key to associate to, the value used is irrelevant. For example, to “store” the number 26 as a key: $pick{26} = 'foobar'; When printing out the 6 random numbers, be sure to sort the keys numerically.

Exercise 6.5 (Optional) Given two lists of non-repeating elements (i.e., no list has the same element more than once), find and print the union and intersection of those lists. For example, = (1, 3, 5, 6, 7, = (0, 2, 3, 5, 7, 9); The union of the values in the two arrays would be: (0, 1, 2, 3, 5, 6, 7, 8, 9) The intersection of the values in the two arrays would be: (3, 5, 7) Use hash variables to build the union and intersection sets.