Download presentation
Presentation is loading. Please wait.
Published byMorgan Hardy Modified over 9 years ago
1
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 3: Collections suggested reading: Learning Perl (4th Ed.), Chapter 3: Lists and Arrays Chapter 6: Hashes
2
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 2 $ Scalar values
3
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 3 $ Single, or scalar, values my $bender = "robot"; my $answer = 42; my $fred = undef;
4
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 4 @ Arrays AKA Lists, Sequences, Tuples
5
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 5 @ Arrays my @array_name = (scalar_1, scalar_2,…, scalar_n); For example… my @futurama = ("Bender", "Fry", "Fry", "Leela");
6
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 6 @ Arrays my @futurama = ("Bender", "Fry", "Fry", "Leela"); To address a single item, you use $ –$futurama[0] is "Bender" –$futurama[1] is "Fry"
7
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 7 @ Arrays Easy: – Read a numbered place print $futurama[1]; – Write a numbered place $futurama[2] = "Zoidberg"; Hard – Find a particular value Where is "Fry"?
8
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 8 @ Arrays Useful for ordered information my @US_Presidents = ("Washington", "Adams", "Jefferson"); my @days_of_week = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
9
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 9 @ Arrays my @array; $array[10] = "Ten!"; $array[0] through $array[9] automatically exist, but are undefined
10
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 10 You can easily add and subtract items; array can resize as needed –push, pop, shift, unshift add and remove from beginning and end of array –delete can delete from the middle – Array slicing returns or modifies subsets @ Arrays
11
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 11 Array flattening Perl flattens arrays my @x = (1, 2); my @y = (9, @x, 9, @x); This is equivalent to my @y = (9, 1, 2, 9, 1, 2);
12
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 12 Array assignment my @array = (1, 2, 3); my($one, $two) = @array; Now $one is 1 and $two is 2 The 3 was just ignored my($a, @b) = @array; Now $a is 1, $b[0] is 2, and $b[1] is 3. @b slurped up the rest
13
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 13 Printing arrays my(@array) = ('a', 'b', 'c'); Print second element: –print "Second: $array[1]"; – Output is " Second: b " Print entire queue: –print "Array: @array"; – Output is " Array: a b c "
14
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 14 You can use loop to print an array nicely, but there is a useful shortcut: my(@array) = ('a', 'b', 'c'); my $string = join(', ', @array); print "Array: $string."; Output: Array: a, b, c. Joining arrays
15
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 15 @ Arrays as ordered information Baseball scores my @scores; for (my $i = 0; $i < 9; $i++) { my $in = $i + 1; print "Inning $in score?\n"; chomp(my $score = ); $scores[$i] = $score; print "Scores: @scores\n"; }
16
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 16 @ Arrays as queues Movies in your Netflix queue my @netflix_q = ("12 Monkeys", "Time Bandits", "Brazil"); my $next_dvd = shift @netflix_q; – $next_dvd is now "12 Monkeys" – @netflix_q is now ("Time Bandits", "Brazil"); push @netflix_q, "Munchausen"; – @netflix_q is now ("Time Bandits", "Brazil", "Munchausen");
17
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 17 @ Arrays as stacks my @commands = ('select', 'bold', 'delete'); my $undo = pop @commands; # $undo is now 'delete' # @commands is now # ('select', 'bold') push @commands, 'italics'; # @commands is now ('select', 'italics')
18
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 18 % Hashes
19
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 19 % Hashes AKA dictionaries, associative arrays, maps my %authors = ( "Dark Tower" => "Stephen King", "Harry Potter" => "J.K. Rowling", "Discworld" => "T. Pratchett", "Johnny" => "T. Pratchett", ); Key Value
20
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 20 % Hashes my %authors = ( "Dark Tower" => "Stephen King", "Harry Potter" => "J.K. Rowling", "Discworld" => "T. Pratchett", "Johnny" => "T. Pratchett", ); Relates scalars to scalars. Keys must be unique
21
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 21 % Hashes my %authors = ( "Harry Potter" => "J.K. Rowling", "Discworld" => "T. Pratchett", ); "Who wrote Discworld?" – Easy: print $authors{"Discworld"}; "Conan was written by Howard." – Easy: $authors{"Conan"} = "Robert Howard";
22
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 22 % Hashes my %authors = ( "Harry Potter" => "J.K. Rowling", "Discworld" => "T. Pratchett", ); "What did Pratchett write?" – Hard: walk the hash looking
23
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 23 % Hashes my %authors = ( "Harry Potter" => "J.K. Rowling", "Discworld" => "T. Pratchett", ); "=>" is (mostly) identical to "," Hash into an array is just the pairs Array into a hash assumes key, value, key, value, etc
24
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 24 % Hashes No inherent order to the keys – Assume they come back in the worst possible order! Useful for associating values to other values. – A series with the author. – A word with its definition. – A username with a password.
25
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 25 Login system A login system: my %passwords = ( 'root' => 'k8H6h%4A', 'bob' => 'secretcode!'); Is the user name valid? –exists($passwords{$username}) Is the password valid? –$passwords{$username} eq $pass
26
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 26 Login system Add a user –$passwords{$newuser} = $newpass; Remove a user –delete $passwords{$olduser};
27
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 27 Can use as a set. Useful for "is this part of the set" questions. Spam filtering: my %spammers = ('malware@example.com' => 1, 'scammer@example.org' => 1); if(exists($spammers{$email}) ) { print "Refuse email from $email: SPAM\n"; } # Variant test: if( $spammers{$email} ) { % Hashes as sets
28
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 28 % Hashes as sets Sets are useful for tracking things seen. my %seen; foreach my $email (@emails) { $seen{$email} = 1; } print join(", ", keys(%seen)); (Shorter forms exist)
29
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 29 $ @ % $foo, @foo, and %foo are three different variables. – Different namespaces. $foo[1] is the second element of @foo $foo{1} is an element of %foo
30
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 30 How big is my array? If you try to use an array where only a scalar makes sense, Perl will return the size of the array –my $size = @array; – or more explicitly… –my $size = scalar(@array); – Very Perl specific!
31
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 31 How big is my hash? " scalar %hash" doesn't work You can use "keys" to get an array of of the indices for the hash. – my $size = scalar(keys(%hash)); – my $size = keys(%hash);
32
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 32 length RIGHT: length("some string") –(It's 11) WRONG: length(@foo) WRONG: length(%foo)
33
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 33 Looping over collections
34
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 34 Loops: foreach # Obviously this works for (my $i = 0; $i < @x; $i++) { print "$x[$i]\n"; } # Sometimes more handy: foreach my $element (@x) { print "$element\n"; }
35
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 35 Loops: foreach foreach lets you modify the original array foreach my $element (@x) { # This actually changes @x! $element = 'Hello'; }
36
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 36 Foreach works on a hash foreach my $key (keys(%x)) { print "$key maps to $x{$key}\n"; } But sometimes it's easier to say while(my($key, $val) = each(%hash)) { print "$key maps to $val\n"; } Loops: each
37
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 37 Other Languages
38
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 38 Perl Arrays @futurama = ( "Bender", "Fry" ); $futurama[1] Hashes %series = ( "Dark Tower" => "King", "Harry Potter" => "Rowling"); $series{"Harry Potter"}
39
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 39 Ruby Arrays futurama = [ "Bender", "Fry" ] futurama[1] Hashes series = { "Dark Tower" => "King", "Harry Potter" => "Rowling"} series["Harry Potter"]
40
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 40 Python Arrays (lists) futurama = [ "Bender", "Fry" ] futurama[1] Hashes (dictionaries) series = { "Dark Tower" : "King", "Harry Potter" : "Rowling"} series["Harry Potter"]
41
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 41 Compared: Array Size Perl: scalar(@array) Python: len(array) Ruby: array.length Javascript: array.length
42
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 42 Compared: Remove and return last item Perl: pop(@array) Python: array.pop Ruby: array.pop Javascript: array.pop()
43
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 43 Merging arrays and hashes Lua, JavaScript, PHP, and others have one type for both – Lua: tables a = {} a["bob"] = "barker" a[1] = "steak sauce" – Javascript: array var a = new Array(); a["bob"] = "barker"; a[1] = "steak sauce";
44
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 44 Python offers a native "set" spammers = set([ \ 'malware@example.com', 'scammer@example.org']) if email in spammers: print "Refusing SPAM\n" Look for variations
45
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 45 Look for variations PHP preserves insert order! $arr[2] = "two"; $arr[3] = "three"; $arr[1] = "one"; foreach ($arr as $element) { print "$element "; } prints: "two three one "
46
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 46 Homework Implementing Metacritic or Rotten Tomatoes – Collect reviewers scores – Report the scores and an average
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.