8.1 References and complex data structures. 8.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

A primer on Perl programming First structures (with examples)
Advanced Perl WUC Perl Poetry Variable Scope.
Lecture 6 More advanced Perl…. Substitute Like s/// function in vi: #cut with EcoRI and chew back $linker = “GGCCAATTGGAAT”; $linker =~ s/CAATTG/CG/g;
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
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.
10.1 References & Complex Data Structures Variable types in PERL ScalarArrayHash $number $string %hash $reference
11.1 Variable types in PERL ScalarArrayHash $number $string %hash $array[0] $hash{key}
Scripting Languages Chapter 5 Hashes. Hash Data structure, not unlike an array – it will hold any number of values It indexes values by name – not by.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
7ex.1 Hashes. 7ex.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Eyal Enter a phone number:
9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
14.1 Wrapping up Revision 14.3 References are your friends…
13.1 Wrapping up Running Other Programs 13.3 You may run programs using the system function: $exitValue = system("blastall.exe..."); if ($exitValue!=0)
8.1 Hashes (associative arrays). 8.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Dudi Enter a phone.
10.1 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
8ex.1 References and complex data structures. 8ex.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
9.1 Hashes. 9.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Ofir Enter a phone number: Enter.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
13r.1 Revision (Q&A). 13r.2 $scalar 13r.3 Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10);
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
Basic Perl Programming
– Intermediate Perl 9/16/ Intermediate Perl - References Intermediate Perl Session 1 · references · complex data structres.
Computer Programming for Biologists Class 7 Nov 27 th, 2014 Karsten Hokamp
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
– Intermediate Perl 10/23/ Intermediate Perl - References Intermediate Perl Session 2 · references, continued · function.
Perl Practical(?)‏ Extraction and Report Language.
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.
Intro to PHP Carl-Erik Svensson. What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
An Intro to Perl, Pt. 2 Hashes, Foreach Control, and the Split Function.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Introduction to Programming the WWW I CMSC Winter 2003.
5 1 Data Files CGI/Perl Programming By Diane Zak.
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.
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
POINTERS.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
8.1 Common Errors – Exercise #3 Assuming something on the variable part of the input file. When parsing a format file (genebank, fasta or any other format),
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
Maps Nick Mouriski.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Perl References arrays and hashes can only contain scalars (numbers and strings)‏ if we want something more complicated (like an array of arrays) we use.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 6: References suggested reading: perlreftut
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
References and Data Structures
Introduction to Perl: Part II
Advanced Topics Web Programming.
Perl Variables: Hash Web Programming.
Presentation transcript:

8.1 References and complex data structures

8.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated with a value. A hash variable name always start with a “%”: my %h = ("a"=>5, "bob"=>"zzz", 50=>"Johnny"); You can access a value by its key: print $h{50}.$h{a};Johnny5 $h{bob} = "aaa"; (modifying an existing value) $h{555} = "z"; (adding a new key-value pair) Hash – an associative array

8.3 To iterate over the keys in %h foreach $key (keys(%h))... For example: foreach $key (keys(%h)) { print "The key is $key\n"; print "The value is $h{$key}\n"; } The elements are given in an arbitrary order, so if you want a certain order use sort: foreach $key (sort(keys(%h)))... Iterating over hash elements

8.4 So far, we know two types of data structures: An Array is an ordered list of scalar values: = ("Shmuel", "Moti", "Rahel"); A Hash is an unordered set of pairs of scalar values: my %phoneBook = ("Shmuel"=>5820, "Moti"=>2745); However, in many situations we may need to store more complex data records. For example – how to keep the phone number, address and list of grades for each student in a course? We would like a data record that looks like this: "Shmuel" => (5820, "34 HaShalom St.", (85,91,67)) For this to work we’re going to need references… Why do we need complex data structures?

8.5 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key} $reference 0x225d14 @array3

8.6 A reference to a variable is a scalar value that “points” to the variable: $nameRef = = (85,91,67); $gradesRef = $phoneBookRef = \%phoneBook; References $phoneBookRef %phoneBook $gradesRef$nameRef$name

8.7 A reference to a variable is a scalar value that “points” to the variable: $nameRef = = (85,91,67); $gradesRef = $phoneBookRef = \%phoneBook; We can make an anonymous reference without creating a variable with a name: [ITEMS] creates a new, anonymous array and returns a reference to it; {ITEMS} creates a hash: $arrayRef = [85,91,67]; $hashRef = {85=>4,91=>3}; (These are variables with no variable name) $gradesRef$arrayRef

8.8 $nameRef = \$name; $gradesRef = $phoneBookRef = \%phoneBook; print $gradesRef;ARRAY(0x225d14) To access the data from a reference we need to dereference it: print $$nameRef;Yossi print $$gradesRef[3] = 100; print $phoneNumber = $$phoneBookRef{"Yossi"}; De-referencing 100 was added to the original $gradesRef

8.9 $gradesRef = $phoneBookRef = \%phoneBook; print $$gradesRef[3] = 100; $phoneNumber = $$phoneBookRef{"Yossi"}; The following notation is equivalent, and sometimes it is more readable: $gradesRef->[3] = 100; $phoneNumber = $phoneBookRef->{"Yossi"}; $gradesRef

8.10 Because a reference is a scalar value, we can store a reference to an array\hash in as an element in another = (85,91,67); %students = (Yossi => $students{Yossi} = $students{Shmuel} = [83,76]; Now the key “Yossi” is paired to a reference value: print $students{Yossi};ARRAY(0x22e714) print print ${$students{Yossi}}[1];91 print $students{Yossi}->[1];91 This form is more readable, we strongly recommend it… References allow complex structures %students NAME => [GRADES] %students =>

8.11 Now we can do it: “how to keep the phone number, address and list of grades for each student in a course?” $students{Yossi} = {phone=>3744, address=>"34 HaShalom St.", grades=>[93,72,87]}; $students{Rahel} = {phone=>5732, address=>"5 Bazel St.", grades=>[91,86,88]}; References allow complex structures %students NAME => {phone => PHONE address => ADDRESS grades => [GRADES]} %students =>

8.12 Now we can do it: “how to keep the phone number, address and list of grades for each student in a course?” $students{Yossi} = {phone=>3744, address=>"34 HaShalom St.", grades=>[93,72,87]}; print $students{Yossi}->{grades}->[2]; 87 It is more convenient to use a shorthand notation: print $students{Yossi}{grades}[2] But remember that there are references in there! References allow complex structures %students NAME => {phone => PHONE address => ADDRESS grades => [GRADES]} %students =>

8.13 The following code is an example of iterating over two levels of the structure – The top hash (each student) and the internal arrays (lists of grades): foreach my $name (keys(%students)) { foreach my $grade { print $grade; } } References allow complex structures %students NAME => {phone => PHONE address => ADDRESS grades => [GRADES]} %students =>

Now we can also create a 2-dimensional array (a table or a = ([1,2,3],[4,5,6],[7,8,9]); print $table[1]->[0];4 Or: print $table[1][0];4 Two dimensional arrays