PERL Variables and data structures Andrew Emerson, High Performance Systems, CINECA.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
A Guide to Unix Using Linux Fourth Edition
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Practical Extraction & Report Language Picture taken from
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
subroutines and references
Computer Programming for Biologists Class 7 Nov 27 th, 2014 Karsten Hokamp
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Continuing with Perl Homework 3: first Perl homework – due Sunday by midnight – one PDF file, by .
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Input, Output, and Processing
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 5 Server Side Scripting Perl.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
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.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Built-in Data Structures in Python An Introduction.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
IDL Tutorial Day 1 Goals: 1) Introduce IDL basics 2) Describe fundamental IDL structures Angela Des Jardins
PERL Ronald L. Ramos Proglan. What is PERL? Perl stands for Practical Extraction and Reporting Language. (or Pathologically Eclectic Rubbish Lister).
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”
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
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,
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.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
PERL SCRIPTING. COMPUTER BASICS CPU, RAM, Hard drive CPU can only use data in the register directly CPU RAM HARD DRIVE.
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.
COMP234-Perl Variables, Literals Context, Operators Command Line Input Regex Program template.
Organization of Programming Languages Meeting 37 April 18, 2016.
String and Lists Dr. José M. Reyes Álamo.
Pamela Moore & Zenia Bahorski
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
An Introduction to Python
Presentation transcript:

PERL Variables and data structures Andrew Emerson, High Performance Systems, CINECA

The “Hello World” program Consider the following: # # Hello World # $message=“Ciao, Mondo”; print “$message \n”; exit; # # Hello World # $message=“Ciao, Mondo”; print “$message \n”; exit;

Perl Variables $message is called a variable, something with a name used to hold one or more pieces of information. All computer languages have the ability to create variables to store and manipulate data. Perl differs from other languages because you do not specify the “type” (i.e. integer, real, character, etc.) only the “complexity” of the data.

Perl Variables Perl has 3 ways of storing data: 1.Scalar  For single data items, like numbers or strings. 2.Arrays  For ordered lists of scalars. Scalars indexed by numbers. 3.Associative arrays or “hashes”  Like arrays, but uses “keys” to identify the scalars.

Scalar Variables Examples # $no_of_chrs=24; # integer $per_cent_identity=0; # also integer $per_cent_identity=99.50; # redefined as real $pi = ; # floating point (real) $e_value=1e-40; # using scientific notation $dna=“GCCTACCGTTCCACCAAAAAAAA”; # string -double quotes $dna=‘GCCTACCGTTCCACCAAAAAAAA’; # string -single quotes # $no_of_chrs=24; # integer $per_cent_identity=0; # also integer $per_cent_identity=99.50; # redefined as real $pi = ; # floating point (real) $e_value=1e-40; # using scientific notation $dna=“GCCTACCGTTCCACCAAAAAAAA”; # string -double quotes $dna=‘GCCTACCGTTCCACCAAAAAAAA’; # string -single quotes

Scalar Variables CASE is important, $DNA ≠ $dna ; (true for all variables) Scalars must be prefixed with a $ whenever they are used (is there a $ ? Yes → it is a scalar). The next character should be a letter and not a number (true for all variables). Scalars can be happily redefined at any time (e.g. integer → real → string): # unlikely example $dna = 0; # integer $dna = “GGCCTCGAACGTCCAGAAA”; # now it’s a # string # unlikely example $dna = 0; # integer $dna = “GGCCTCGAACGTCCAGAAA”; # now it’s a # string

Doing things with scalars.. # $a =1.5; $b =2.0; $c=3; $sum = $a+$b*$c; # multiply by $b by $c, add to $a # while ($j<100) { $j++; # means $j=$j+1, i.e. add 1 to j print “$j\n”; } # $dna1=“GCCTAAACGTC”; $polyA=“AAAAAAAAAAAAAAAA”; $dna1.= $polyA; # add one string to another # (equiv. $dna1 = $dna1.$polyA) $no_of_bases = length($dna2); # length of a scalar # $a =1.5; $b =2.0; $c=3; $sum = $a+$b*$c; # multiply by $b by $c, add to $a # while ($j<100) { $j++; # means $j=$j+1, i.e. add 1 to j print “$j\n”; } # $dna1=“GCCTAAACGTC”; $polyA=“AAAAAAAAAAAAAAAA”; $dna1.= $polyA; # add one string to another # (equiv. $dna1 = $dna1.$polyA) $no_of_bases = length($dna2); # length of a scalar

More about strings.. There is a difference between strings with ‘ and “ # $nchr = 24; $message=“chromosones in human cell =$nchr”; print $message; $message = ‘chromosones in human cell =$nchr’; print $message; exit; # $nchr = 24; $message=“chromosones in human cell =$nchr”; print $message; $message = ‘chromosones in human cell =$nchr’; print $message; exit; single quotes double quotes OUTPUT chromosones in human cell =24 chromosones in human cell =$nchr OUTPUT chromosones in human cell =24 chromosones in human cell =$nchr

More about strings Double quotes “ interpret variables, single quotes ‘ do not: $dna=‘GTTTCGGA’; print “sequence=$dna”; print ‘sequence=$dna’; $dna=‘GTTTCGGA’; print “sequence=$dna”; print ‘sequence=$dna’; OUTPUT sequence=GTTTCGGA sequence=$dna OUTPUT sequence=GTTTCGGA sequence=$dna Normally you would want double quotes when using print.

‘tue’, = (‘adenine’, ‘guanine’, ‘thymine’, ‘cytosine’, ‘DEFINITION’, ‘tue’, = (‘adenine’, ‘guanine’, ‘thymine’, ‘cytosine’, ‘DEFINITION’, ‘ACCESSION’,... ); Arrays Collections of numbers, strings etc can be stored in arrays. In Perl arrays are defined as ordered lists of scalars and are represented with character. Initializing arrays with lists

Arrays - elements To access the individual array elements you use [ and ] o’,’lys’,’ser’,’phe’); # now mutate the peptide $poly_peptide[0]=‘val’; $i=0; # print out what we have while ($i<8) { print “$poly_peptide[$i] “; $i++; o’,’lys’,’ser’,’phe’); # now mutate the peptide $poly_peptide[0]=‘val’; $i=0; # print out what we have while ($i<8) { print “$poly_peptide[$i] “; $i++; } Look array index The numbers used to identify the elements are called indices.

Arrays - elements When accessing array elements you use $ - why ? Because array elements are scalar and scalars must have $poly_peptide[0] = $poly_peptide[0] = ‘val’; This means that you can have a separate variable called $poly_peptide because $poly_peptide[0] is part NOT $poly_peptide. This may seem a bit weird, but that's okay, because it is weird. Unix Perl Manual

Array indices start from 0 not 1 ; Array elements $poly_peptide[0]=‘var’; $poly_peptide[1]=‘ser’; $poly_peptide[7]=‘phe’; $poly_peptide[0]=‘var’; $poly_peptide[1]=‘ser’; $poly_peptide[7]=‘phe’; The last index of the array can be found from $#name_of_array, e.g. $#poly_peptide. You can also use negative indices: it means you count back from the end of the array. Therefore $poly_peptide[-1]= $poly_peptide[$#poly_peptide] = $poly_peptide[7]

Array properties Length of an array: $len = $#poly_peptide+1; The size of the array does not need to be defined – it can grow dynamically: # begin program $i=0; while ($i<100) { $polyA[$i]=‘A’; $i++; } # begin program $i=0; while ($i<100) { $polyA[$i]=‘A’; $i++; }

Useful Array functions PUSH and POP Functions commonly used for manipulating a stack: PUSH POP F.I.L.O = First In Last Out Very common in computer programs

Array functions – PUSH and POP # part of a program that reads a database into an array # open database etc # while ($line= ) { # push $line onto array }... while { $record = # pop line off and use it.... do something } # part of a program that reads a database into an array # open database etc # while ($line= ) { # push $line onto array }... while { $record = # pop line off and use it.... do something }

Scalar Contexts If you provide an expression (e.g. an array) when Perl expects a scalar, Perl attempts to evaluate the expression in a scalar context. For an array this is the length of an array: ; $length=$#poly_peptide+1; This is equivalent to Hence: while {.. while {.. array in scalar context = length of array

Special variables $_ Set in many situations such as reading from a file or in a foreach loop. $0 Name of the file currently being executed. $] Version of Perl being Contains the parameters passed to a Contains the command line arguments passed to the program. Perl defines some variables for special purposes, including: Some are read-only and cannot be changed: see man perlvar for more details.

Associative Arrays (Hashes) Similar to normal arrays but the elements are identified by keys and not indices. The keys can be more complicated, such as strings of characters. Hashes are indicated by % and can be initialized with lists like arrays: %hash = (key1,val1,key2,val2,key3,val3..);

Associative Arrays (Hashes) Examples %months=(‘jan’,31,’feb’,28,’mar’,31,’apr’,30); Alternatively, %months=(‘jan’=> 31, ’feb’=> 28, ’mar’=> 31, ’apr’=> 30); %months=(‘jan’=> 31, ’feb’=> 28, ’mar’=> 31, ’apr’=> 30); => is a synonym for, key value

Associative Arrays (Hashes) Further examples # %classification = (‘dog’ => ‘mammal’, ‘robin’ => ‘bird’, ‘snake’ => ‘reptile’); %genetic_code = ( ‘TCA’ => ‘ser’, ‘TTC’ => ‘phe’, ‘TTA’ => ‘leu’, ‘TTA’ => ‘STOP’ ‘CCC’ => ‘pro’,... ); # %classification = (‘dog’ => ‘mammal’, ‘robin’ => ‘bird’, ‘snake’ => ‘reptile’); %genetic_code = ( ‘TCA’ => ‘ser’, ‘TTC’ => ‘phe’, ‘TTA’ => ‘leu’, ‘TTA’ => ‘STOP’ ‘CCC’ => ‘pro’,... );

The elements of a hash are accessed using curly brackets, { and } : Associative Arrays (Hashes) - elements $genetic_code{TCA} = ‘ser’; $genetic_code{CCC} = ‘pro’; $genetic_code{TGA} = ‘STOP’; $genetic_code{TCA} = ‘ser’; $genetic_code{CCC} = ‘pro’; $genetic_code{TGA} = ‘STOP’; Note the $ sign: the elements are scalars and so must be preceded by $, even though they belong to a % (just as for arrays).

Associative Arrays (Hashes) – useful functions exists indicates whether a key exists in the hash if (exists $genetic_code{$codon}) {... }else { print “Bad codon $codon\n”; exit; } if (exists $genetic_code{$codon}) {... }else { print “Bad codon $codon\n”; exit; }

Associative Arrays (Hashes) – useful functions keys and values makes arrays from the keys and values of a = keys = values = keys = values %genetic_code; Often you will see code like the following: foreach $codon (keys %genetic_code) { if ($genetic_code{$codon} eq ‘STOP’) { last; # i.e. stop translating } else { $protein.= $genetic_code{$codon}; } foreach $codon (keys %genetic_code) { if ($genetic_code{$codon} eq ‘STOP’) { last; # i.e. stop translating } else { $protein.= $genetic_code{$codon}; }