1.1.2.8 – Intermediate Perl 9/16/20151.1.2.8.1 - Intermediate Perl - References 1 1.1.2.8.1 Intermediate Perl Session 1 · references · complex data structres.

Slides:



Advertisements
Similar presentations
A primer on Perl programming First structures (with examples)
Advertisements

Advanced Perl WUC Perl Poetry Variable Scope.
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.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
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.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Combining Like Terms Students will identify like terms and combine like terms to solve problems.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
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
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
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.
– Introduction to Perl 10/20/ Introduction to Perl - Lists and Arrays Introduction to Perl Session 3 · lists and arrays.
– Intermediate Perl 10/23/ Intermediate Perl - References Intermediate Perl Session 2 · references, continued · function.
– Introduction to Perl 10/24/ Introduction to Perl - Hashes and Sorting Introduction to Perl Session 4 · hashes · sorting.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
References. Analagous (somewhat) to pointers in C/C++ –Far less messy, and definitely less dangerous –Variables that "refer" to another variable You can.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Basics. sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”;
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
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)
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
Pointers *, &, array similarities, functions, sizeof.
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 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.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
Interpolation Variable Interpolation, Backslash Interpolation.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
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.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
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
References and Data Structures
Pointers What is the data type of pointer variables?
Introduction to Perl: Part II
Intermediate Perl - References
References and Pointers
Where’s the pencil?.
Presentation transcript:

– Intermediate Perl 9/16/ Intermediate Perl - References Intermediate Perl Session 1 · references · complex data structres

– Intermediate Perl 9/16/ Intermediate Perl - References 2 · references are scalars which point to variables · they hold the variable’s memory location · value of the variable can be obtained by dereferencing · size of scalar variable is always the same size, regardless of type of variable · easy to pass around a reference, instead of large variable References a horse referent horse this way reference

– Intermediate Perl 9/16/ Intermediate Perl - References 3 References Point to Memory Addresses · perl’s basic variable types are and %hashes · each variable may occupy different amounts of memory · each variable can have a reference $a = = (1,2,3) %c = (one=>1,two=>2) %c $a_ref = \$a $b_ref = $c_ref = \%c

– Intermediate Perl 9/16/ Intermediate Perl - References 4 Creating References scalar $x hash %h reference$x_ref = \$x$a_ref = = \%h · to create a reference add \ at the front of the variable · to dereference, add the appropriate variable prefix to the reference · e.g. if dereferencing an array reference · we'll leave scalar references for now, since they're least used

– Intermediate Perl 9/16/ Intermediate Perl - References 5 Rule #1 · let $v_ref be a reference to variable · you can replace variable by {$v_ref} in any = (1,2,3); $a_ref = %h = (one=>1,two=>2); $h_ref = \%h; print %h print %{$h_ref} · %{$v_ref} → %$v_ref reference dereference drop { } and take advantage of precedence

– Intermediate Perl 9/16/ Intermediate Perl - References 6 References as Strings and Ref() my $x = 10; = (1,2,3); my %h = (one=>1,two=>2); my $x_ref = \$x; my $a_ref = my $h_ref = \%h; print $x;10 print $x_ref;SCALAR(0x80cb34c) print $a_ref;ARRAY(0x80cb358) print %h;two2one1 print $h_ref;HASH(0x80cf3d4) · If you try to print a reference variable, you get a text string · the string is useless, except for debugging · it indicates the referent type · it indicates the memory address

– Intermediate Perl 9/16/ Intermediate Perl - References 7 Identifying References with Ref() · the function ref() is used to identify references · ref($var) returns undef if $var is not a reference · ref($var) returns the string "SCALAR", "ARRAY", or "HASH" print ref($x); print ref($x_ref);SCALAR print ref($a_ref);ARRAY print ref($h_ref);HASH if (ref($mystery_variable) eq "ARRAY") { } else { print "not an array reference"; }

– Intermediate Perl 9/16/ Intermediate Perl - References 8 Anonymous References – Part 1 · previously, we needed a pre-existing variable to create a reference. · suppose I want a reference to the list (1,2,3) · once the reference is is no longer needed · (1,2,3) can be accessed · an anonymous reference obviates the need for a named = (1,2,3) $a_ref = $a_ref = [1,2,3];

– Intermediate Perl 9/16/ Intermediate Perl - References 9 Anonymous References – Part 2 · anonymous references are going to initially annoy you because they add yet another set of bracket rules · train your eyes · keep patient · collect rewards = (1,2,3) hash %h = (one=>1,two=>2) reference created from variable $a_ref = = \%h anonymous reference$a_ref = [1,2,3]$h_ref = {one=>1,two=>2}

– Intermediate Perl 9/16/ Intermediate Perl - References 10 Anonymous References – Part 3 · to recover the variable refered by an anonymous reference – dereference! · remember, there is no associated named = (1,2,3); $ar1 = reference points to named variable $ar2 = [1,2,3]; anonymous reference does not point to any named variable %h = (one=>1,two=>2) $hr1 = \%h $hr2 = {one=>1,two=>2};

– Intermediate Perl 9/16/ Intermediate Perl - References 11 Perl Tattoo ()[]{} list or hash anonymous list reference anonymous hash reference list reference hash reference %h=(one=>1,two=>2) $ar = $hr = $ar = [1,2,3] $hr = {one=>1,two=>2}

– Intermediate Perl 9/16/ Intermediate Perl - References 12 References for Complex Data Structures · to create complex data structures (e.g. lists of lists) you need references because · list elements must be scalars · hash values must be scalars · remember that lists collapse · ((1,2),(3,4)) not a two-element list of lists but a 4-element list of scalars · a list of lists is created by making a list of references to lists # these are the = ( (1,2),(3,4) = (1,2,3,4)

– Intermediate Perl 9/16/ Intermediate Perl - References 13 Dereferencing – Lists · to dereference $a_ref $a_ref = [1,2,3] ${$a_ref}[0] $a_ref->[0] ${$a_ref}[1] $a_ref->[1] ${$a_ref}[2] $a_ref->[2] “go blind” method “stay sane” method $a[0] $a[1] $a[2] compare with for

– Intermediate Perl 9/16/ Intermediate Perl - References 14 Dereferencing – Hashes · to dereference $h_ref $h_ref = {one=>1,two=>2} ${$h_ref}{one} $h_ref->{one} ${$h_ref}{two} $h_ref->{two} “go blind” method “stay sane” method $h{one} $h{two} $h{three} compare with for hash %h

– Intermediate Perl 9/16/ Intermediate Perl - References 15 = (1,2,3) hash %h = (one=>1,two=>2) reference created from variable $a_ref = = \%h anonymous reference$a_ref = [1,2,3]$h_ref = {one=>1,two=>2} dereferencing $a_ref->[0] $a_ref->[1] $a_ref->[2] $h_ref->{one} $h_ref->{two} Reference table to references

– Intermediate Perl 9/16/ Intermediate Perl - References 16 · consider the following hash, whose values are list references · $h is a reference to a hash of lists · $h->{even} is the value of the “even” key which is $even, a list reference Deeply Nested Structures $even = [2,3,4]; $odd = [1,3,5]; $h = {even=>$even,odd=>$odd}; $h reference to hash $h->{even}reference to value of good $h->{even}->[0] first element in value of even (2) $h->{even}[0]first element in value of even (2) $h->{odd}[0]first element in value of odd (1) $h->{odd}[1]second element in value of odd (3) $h->{odd}[2]third element in value of odd (5)

– Intermediate Perl 9/16/ Intermediate Perl - References 17 More Deeply Nested Structures $f1 = { apple => [qw(red tasty)] } $f2 = { banana => [qw(yellow squishy)] } $f3 = { papaya => [qw(green gooey)] } $bag1 = [$f1,$f2]; $bag2 = [$f2,$f3]; $box = [$bag1,$bag2]; $f1->{apple} ARRAY(0x) (reference to anonymous list) $f1->{apple}[0] red $bag1->[0] HASH(0x) (reference to $f1) $bag1->[0]{apple} ARRAY(0x) (reference to anonymous list) $bag1->[0]{apple}[1] tasty $box->[1][1]{papaya}[0]green ${${${${$box}[1]}[1]}{papaya}}[0]I’m blind box bag1 f1 f2 f3

– Intermediate Perl 9/16/ Intermediate Perl - References 18 More Deeply Nested Structures $f1 = { apple => [qw(red tasty)] } $f2 = { banana => [qw(yellow squishy)] } $f3 = { papaya => [qw(green gooey)] } $bag1 = [$f1,$f2]; $bag2 = [$f2,$f3]; $box = [$bag1,$bag2]; $box->[1][1]{papaya}[0] box bag1 f1 f2 f3 papaya green gooey KEY VALUE

– Intermediate Perl 9/16/ Intermediate Perl - References 19 Iterating through the Box of Bags of Fruits $f1 = { apple => [qw(red tasty)] }; $f2 = { banana => [qw(yellow squishy)] }; $f3 = { papaya => [qw(green gooey)] }; $bag1 = [$f1,$f2]; $bag2 = [$f2,$f3]; $box = [$bag1,$bag2]; for $bag { # each $bag is a list reference for $fruit { # each $fruit is a hash reference for $fruit_name (keys %$fruit) { # each $fruit_prop is a list reference for $fruit_prop { print “$fruit_name is $fruit_prop”; }

– Intermediate Perl 9/16/ Intermediate Perl - References Intermediate Perl Session 1 · [] is for array lookup · [] is for anonymous arrays · {} is for hash key lookup · {} is for anonymous hashes