CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 6: References suggested reading: perlreftut

Slides:



Advertisements
Similar presentations
Perl Practical Extration and Reporting Language An Introduction by Shwen Ho.
Advertisements

Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
PHYS 2020 Pseudocode. Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear.
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.
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
Hash Tables and Associative Containers CS-212 Dick Steflik.
©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.
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.
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
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.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open, References Perl modules Homework 2: due next Monday by midnight.
subroutines and references
– Intermediate Perl 9/16/ Intermediate Perl - References Intermediate Perl Session 1 · references · complex data structres.
COMPUTATION WITH STRINGS 4 DAY 5 - 9/05/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
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
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
CS212: DATA STRUCTURES Lecture 1: Introduction. What is this course is about ?  Data structures : conceptual and concrete ways to organize data for efficient.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Topic 25 - more array algorithms 1 "To excel in Java, or any computer language, you want to build skill in both the "large" and "small". By "large" I mean.
A very basic overview of Server-Side Scripting Or what is PHP, Perl, Python, Ruby and what can they do for me?
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS107 References and Arrays By Chris Pable Spring 2009.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 3: Collections suggested reading: Learning Perl (4th Ed.), Chapter 3:
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.
Case study Students. Array of objects Arrays can hold objects (ref to objects!) Each cell in an array of objects is null by default Sample: from student.
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.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
5 1 Data Files CGI/Perl Programming By Diane Zak.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
CS 330 Class 9 Programming plan for today: More of how data gets into a script Via environment variables Via the url From a form By editing the url directly.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
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.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
Data Structures and Algorithms in Java AlaaEddin 2012.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Object Oriented Programming Programming Perl Chapter 12: "Objects"
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.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Intro to Data Structures Concepts ● We've been working with classes and structures to form linked lists – a linked list is an example of something known.
References and Data Structures
Component 1.6.
Introduction to Perl: Part II
Hashing Exercises.
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
CISC101 Reminders Slides have changed from those posted last night…
CSCI 3333 Data Structures Array
String and Lists Dr. José M. Reyes Álamo.
CS2011 Introduction to Programming I Arrays (I)
C021TV-I2-S2.
More Variables and Data Types: references/matricies
Introduction to Python
Presentation transcript:

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 6: References suggested reading: perlreftut or "perldoc perlreftut"

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 2 Turn In Homework

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 3 Yesterday's Homework

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 4 What we have $ A scalar – Holds one An array – Holds a bunch of scalars. % A hash – Holds a bunch of scalars. What if I need something more complex?

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 5 References (Similar to pointers, for you C and Pascal fans)

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 6 A reference is a scalar that refers to some other variable my $a = 10; my $b = \$a; ${$b} = 5; print $a; # prints "5" References $a: 10 $b:

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 7 You can point a scalar at a named variable: my $array_ref = my $hash_ref = \%hash; my $scalar_ref = \$scalar; Creating (1,2,3) $array_ref:

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 8 You can point a scalar at an anonymous array or hash: my $array_ref = [ 1, "two", 3.0 ]; my $hash_ref = { "one" => 1, "two" => 2 }; Creating references (1,2,3) $array_ref:

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 9 Using references Put ${} around a scalar ref to look inside. my $scalar_ref = \$scalar; –$scalar and ${$scalar_ref} are identical $scalar = 1; ${$scalar_def} = 2; print "$scalar == 2\n";

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 10 Using and %{} work the same way. Remember to use $ to look at individual entries! my $array_ref are identical $array[2] and ${$array_ref}[2] are identical

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 11 Using references my $hash_ref = \%hash; %hash and %{$hash_ref} are identical $hash{'test'} and ${$hash_ref}{'test'} are identical

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 12 Abbreviations: -> These are identical –$array[2] –${$array_ref}[2] –$array_ref->[2] These are identical –$hash{'test'} –${$hash_ref}{'test'} –$hash_ref->{'test'}

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 13 Abbreviations: [1][2] {1}{2} You can omit the -> between indices These are identical –${${$x[1]}{'fred'}}[9] –$x[1]->{'fred'}->[9] –$x[1]{'fred'}[9]

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 14 Nesting You can nest these forever. – Or until you run out of memory. – Whichever comes first.

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 15 Uses

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 16 Grades (Hash of arrays) There are 5 homework assignments, each is worth 0, 1, or 2 points. Here are my grades: = (0,1,1,2,2); There are multiple students. my(%class) = ( 'Alan' => 'Nick' => [2,1,2,2,2], 'Tim' => [2,2,2,2,2], );

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 17 Let's run the averages foreach my $student (key %class) { my $grade_ref = $class{$student}; my $total = 0; foreach my $score { $total += $score; } my $average = $total / print "$student: $average\n"; }

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 18 Bowling (Array of arrays) Bowling is broken into 10 frames, each with 1, 2, or occasionally 3 balls. You score for each ball. Easily held in an array of arrays! = ([5,0], [7,1], [10], [0,0], [3,0], [4,0], [0,4], [3,4], [10], [0,0]);

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 19 Bowling (Hash of arrays of arrays) $bowling{'Alan'}[1][0] = 5; $bowling{'Alan'}[1][1] = 0; $bowling{'Alan'}[2][0] = 7; $bowling{'Alan'}[2][1] = 3; my(%bowling) = ( 'Alan' => [ [5,0], [7,3] ], 'Joe' => [ [10,0], [10,0] ], 'Chris' => [ [8,1], [8,1] ], );

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 20 Structured data Perl has no direct equivalent to a class or struct in Java/C++ Use hashes (of hashes) to store structured data $movies{'Brazil'}{'Director'} = 'Terry Gilliam'; $movies{'Brazil'}{'Actors'}[0] = 'Jonathan Pryce'; Perl's object system built on this

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 21 More complex data structures Linked lists – Or you can use an array Trees – Or you can use a hash Directed graphs – You could use a hash

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 22 Linked List Example Really, just use an array

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 23 Passing into functions How to pass two arrays to a single = = Turns into (1,2,3,4)!

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 24 Passing into functions sub myfunc { my($a1_ref, $a2_ref) }

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 25 Passing out of functions What if you want to modify the thing passed in. (chomp!) $string = "test\n"; my_chomp(\$string); sub my_chomp { if(substr(${$_[0]}, -1, 1) eq "\n") { ${$_[0]} = substr(${$_[0]}, 0, - 1); }

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 26 Other languages Structured data via class/object – Ruby, Python, Javascript, etc. – Typically: variable.member_variable

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 27 Python Nested data structures Just Work # Array holding an array a = [1, 2, ['3.1', '3.2]', 4] # a[2][0] is '3.1' # Hash holding an array d = {'a' : 'b', 'c' : [1,2] } # d['c'][1] is 2

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 28 Ruby Nested data structures Just Work # Array holding an array a = [1, 2, ['3.1', '3.2]', 4] # a[2][0] is '3.1' # Hash holding an array d = {'a'=> 'b', 'c'=> [1,2] } # d['c'][1] is 2

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 29 Ruby and Python references Like Java, variables for objects are references Hashes and arrays are objects a = [1, 2] b = a b[0] = 'fred' # Modified a[0]!

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 30 So how do you copy arrays and hashes in Ruby and Python? Python: array_b = list(array_a) hash_b = hash_a.copy() Ruby array_b = array_a.clone() hash_b = hash_b.clone() These are shallow copies! – Sub arrays and hashes are still shared!

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 31 Some Philosophy

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 32 The scripting mindset Your time versus the computer's Computers are fast – Moore's law says next month's computer is 10% faster* * Moore's law says nothing of the sort When in doubt, waste the computer's time, not yours Scripting languages based around this idea

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 33 Clarity, Correctness, and Efficiency Sometimes you make tradeoffs The tradeoffs vary based on the task A good general rule 1. Clarity 2. Correct 3. Efficient

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 34 …premature optimization is the root of all evil. - Donald Knuth (paraphrased)