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.

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

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
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.
Bioinformatics Lecture 7: Introduction to Perl. Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration.
Stacks.
Places To Put Things Exploring Perl's Built-In Variable Containers: Arrays and Hashes.
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.
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.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 5 Server Side Scripting Perl.
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.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
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.
Chapter 9: Perl Programming Practical Extraction and Report Language 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
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
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.
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.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
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.
Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
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.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
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.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
String and Lists Dr. José M. Reyes Álamo.
Psuedo Code.
Perl Variables: Array Web Programming.
An Introduction to Perl
Data Structures – 1D Lists
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: part II hashes, foreach control statements, and the split function By: Kevin Walton.
String and Lists Dr. José M. Reyes Álamo.
Class Examples.
Arrays.
Presentation transcript:

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 increase/ decrease while the program is being executed. Each element is a scalar variable An array in perl is declared as follows:

Print Array contents Declaration of an array in perl ; empty array efine (declare and assign values) an array = (‘ATC’, ‘GTC’, ‘AAT’); – This Array contains 3 strings (set of 3 letters: codons!!! Print the values of a non-empty array – Using “For” loops 3 possible ways: If know number of elements of array: standard for-loop If we do not know the number of elements we can: 1.Get length of array and use it with a for loop 2.Foreach - loop

3 ways to access elements of array PrintArray.pl example Sample Code: = ( 'TTATTATGTT', 'GCTCAGTTCT', 'GACCTCTTAA', 'CTATGCGGTA', 'ATCTGACCTC' ); #print values using standard for loop for (my $index = 0; $index < 5; $index++) {print "$sequences[$index]\n"; } for (my $index = 0; $index < ; $index++) foreach my ) { print "$value\n"; }

Input (add) elements to array Perl uses dynamic array which means you can add/remove elements when the program is running. This is a very useful attribute of Perl The elements can be added to the beginning or end of the array. Step 1: – Create an can be empty or contain data Step 2 – Add elements to the array using the: Push function : adds to end of an array : $sequence; Unshift function: adds to the beginning of an array: $sequence; – See Example PushUnshift.pl – This adds elements enter by the user to the end/beginning of the array.

Remove elements from an array To remove an element from an array you can use two functions (reserved words). – Pop removes last element of array and assigns it (returns) to a variable $last = – Shift: removes and returns the first element of an array. $first = – Refer to PopShift.pl

Remove Elements: splice and slice To “assigning and remove” element(s) from anywhere in the array: = 1, 2 The first value is where the splice begins and the second is how many elements are to be removed = [1.. 3] = (‘a’, ‘t’, ’g’,’c’) What is the value after splice – slicing (assigning without removal): = = (‘a’, ‘t’, ’g’,’c’) What is the value – Refer: splice_slice.pl

Dynamic Arrays Other Array operations (refer to : splice_slice.pl) – $one_seq {zero based array} The 3 rd element of the array is assigned to scalar variable All the elements of one array are assigned to the other

Arrays and Files The advantage of using perl’s dynamic arrays is when you need to read the contents of a file of different lengths. – Use a while loop – while { – chomp; – – An approach is “slurping”: (slurping will not remove the carriage return) = ;

Arrays: two more functions Sort Function (sorts in alphabetical order) – Be aware of Capital letters as ‘A’ is not considered the same as ‘a’ – Can use uc (converts to upper case) or lc (convert to lower case) = Reverse Function: reverses the elements (does not reverse the contents of the elements) = The reverse function can also be used with scalar variables: – $reverse = reverse $string; Sort_Reverse.pl

Array and Lists Lists are sets of constants or variables. – Values of a list assigned to any array = (’192a8’,’18c10’,’327h1’,’201e4’); – Values in an array assigned to a list of variables ($first,$second,$third) – The can be used like arrays but you refer to them by variable name as opposed to element number

Exercise 1 #! /usr/bin/perl # The 'pushpop' program - pushing, popping, shifting and = ( 'TTATTATGTT', 'GCTCAGTTCT', 'GACCTCTTAA', 'CTATGCGGTA', 'ATCTGACCTC' ); print $last = print $first = print $last; print ( $first, $last ); What is the expected output (run code to confirm)

Exercise 2 Read the contents of a 2 FASTA Files into an 2 arrays via while loop and slurping. Print the contents of each Remove the first element (line) of each array. Reverse the second array Print the contents of the second array. Combined both arrays and print the contents to a third file which you will call file3.fasts

Exercise 3 Prompt the user for the name of 2 fasta files and read their contents into 1 array. splice the descriptor line (the first line) of each file from the array and print the contents to confirm they have been removed. Ask the user to add three new sequences to the end of the array Sort the array in reverse order Ask the user to input a descriptor line stating their name and place of work ; e.g. “Denis Manley D.I.T. Kevin Street” Add this descriptor line to the beginning of the array. Print the contents the array to a file and print a report to the screen stating how many characters and lines were written to the file.