Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an email from me.

Similar presentations


Presentation on theme: "LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an email from me."— Presentation transcript:

1 LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong

2 Administrivia Homework 1 graded – you should have gotten an email from me

3 Continuing with Perl Learn Perl – Books… – Online resources http://learn.perl.org/ we begin with... http://perldoc.perl.org/perlintro.html

4 Perl arrays and hashes Scalars: – strings, numbers (integers, floating point numbers), references Arrays: – Idea: list of scalars – Access by index: 0,1,2,… Hash: – Like an array except access not through a numeric index – Use user-specified keys $variable @variable %variable different namespaces: $apple @apple %apple are different data structures and can co-exist simultaneously different namespaces: $apple @apple %apple are different data structures and can co-exist simultaneously

5 Perl Arrays Arrays: – Idea: list of scalars – Access by index: 0,1,2,… Notes on output print @azeroonetwothreefour print “@a”zero one two three four controlled by system variable $” default value: a space controlled by system variable $” default value: a space negative indices count from the end.. $#name (index of last element) @name (can have scalar interpretation) negative indices count from the end.. $#name (index of last element) @name (can have scalar interpretation)

6 Perl Arrays Forgot to mention last time, a special array called @ARGV Getting user input into your program: @ARGV C programming language: int argc, char *argv[] Shell script: $0 $1 $2..( $# )

7 Perl arrays List operations:

8 Perl Arrays Last time: – by default sort works according to the ASCII chart, character by character asciitable.com

9 Perl Arrays Numeric sort? Idea: to pass an inline comparison function as parameter… Note: fc (fold case) from Perl 5.16 onwards only

10 Perl Hashes

11 Notes on arrays and hashes – arrays are indexed from 0,1,2,3… – hashes are like arrays with user-defined indexing (aka associative array or hash table) – initialization (use list notation (or shortcut): round brackets and commas) @a = (“zero”, “one”, “two”, “three”, “four”); %h = (“zero”, 0, “one”, 1, “two”, 2, “three”, 3, “four”, 4}; (key/value pairs) – access to individual elements (square brackets vs. curly braces) $a[1]“one” $h{zero}0 Shortcut:

12 Perl Hashes Notes on arrays and hashes – output print @a zeroonetwothreefour print “@a” zero one two three four print %h three3one1zero0two2four4 (note: different order) print “%h” %h (literal, no interpolation done) What happens here? –%pos = ("apple", "n", "speak", "v", "happy", "a", "walk", "n", "walk", "v"); –print $pos{"walk"}, "\n"; (hash keys are unique)

13 Perl Hashes Scalar interpretation of @array: –if ( @a < 10 ) { print “Small array\n” } else {print “Big array\n” } – Note: @a here is a scalar = size of array –unless (@a > 10) { print “@a\n” } – Note: if size of array a is ≤ 10, it prints the contents of array a Looping: gives output: lemon => yellow apple => green orange => orange Note: keys %fruits = (“lemon” “apple” “orange”) produces a list

14 Perl Hashes Sorting sort byage keys %age

15 Implicit Coercion Example: – the following program prints Equal! – == is the numeric equality comparison operator my $one_string = “1”; my $one_number = 1; if ($one_string == $one_number) { print “Equal!\n” } else { print “Different!\n” } Example: – the following program prints 3 is my number –. is the string concatenation operator my @a = qw(one, two, three); my $string = @a.“ is my number”; print “$string\n”; Perl features implicit coercion of data types

16 Implicit Coercion print “4” * 4 16 (strange behavior if you’re coming from Python) print “4” x 4(“x” is the repetition operator) 4444 @a = (4) x 4(in list context) (4, 4, 4, 4)

17 Arrays as Stacks and Queues Arrays: – insertion and deletion from the ends 012n-1n-2 shift/unshift pop/push Generalized form Perl functions may have side effects and also return values


Download ppt "LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an email from me."

Similar presentations


Ads by Google