









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










When a hash is an argument to a subroutine, it is converted to an array and the array is added to the argument list. When a hash is returned from a subroutine, it is converted to an array! This is slooooow! When we use hashes as functions, we have some limits: %a = (%b, %c) One cannot pass two hashes to a subroutine! They will be automagically merged (in the same way that merges %b and %c). Passing hashes to a subroutine is slow! They are converted to arrays, and then back to hashes! Thus Limits of hashes Docsity.com
No nested structures. Representing data was painful. I started programming in Perl 4 References were added to allow nested structure. Without changing anything else. In Perl 5, Result: a very strange syntax! Historical aside Docsity.com
To put hashes or arrays inside hashes or arrays! To create data structures that act like classes. To pass a hash to a function as a scalar. To return a hash from a function as a scalar. Uses of references Uses of references Docsity.com
Concept Normal Reference defining an array @a = (1,2,3); $a=[1,2,3]; accessing an element $a[1] $a->[1] (or ${$a}[1]) getting the size scalar(@a) scalar(@$a) iterating over foreach $k (@a) {…} foreach $k (@$a) {…} copying @b=@a; $b= [@$a]; ($b=$a would alias) defining a hash %h = ("g"=>1, "h"=>2); $h = { "g"=>1, "h"=>2}; accessing an element $h{"g"} $h->{"g"} (or ${$h}{"g"}) getting the keys keys %h keys %$h deleting an element delete $h{"g"}; delete $h->{"g"}; Iterating while (($k,$v) = each %h) {…} while (($k,$v) = each %$h) {…} copying %g=%h; $g={%$h}; ($g=$h would alias) Syntax of references Docsity.com
my $out = {}; # empty reference to hash $out->{$k}=1; foreach my $k (keys %$h) { } return $out; # reference! } Docsity.com
use Data::Dumper; # once: loads the library function print Dumper($a); # prints a depiction of a structure print Dumper(%h); # prints the structure of a hash print Dumper(@a); # prints the structure of an array
How to debug references: Data::Dumper A reference power-tool: Data::Dumper Docsity.com
Any relation { (x,y) } can be converted into a function from elements of x to subsets of y's. The latter is a nested structure. Example: ["amy", "cars"], ["george", "trucks"], ["george", "cars"], ["george", "aardvarks"], ["sue", "cars"], ["sue", "ducks] @relation = ( ); "amy" => { "cars" => 1}, "trucks" => 1, "cars" => 1, "aardvarks" => 1 "george" => { } , "cars" => 1, "ducks" => 1 "sue" => { } $function = { }; After this, $function->{"george"}->{"trucks"} is 1, Application of nested hashes Docsity.com
After this, $function->{"george"}->{"trucks"} is 1, which means that "george likes trucks." This is the code that does it: $left = $r->[0]; $right = $r->[1]; $function->{$left}->{$right}=1; foreach my $r (@relation) { } or, if you're me, and lazy: $function->{$r->[0]}->{$r->[1]}=1; foreach my $r (@relation) { } Docsity.com
Theorem: Any relation R A x B is equivalent to a function from A to subsets of B. Proof: We need to show that any relation is such a function, and that any such function can be converted back into a relation. f(a) = { b ϵ B | (a,b) ϵ R}. From relation to function: Let R be a relation, i.e., a subset of A x B. For each a ϵ A, let Then f(a) is uniquely determined, so that f is a function. Let R = { (a,b) | b ϵ f(a) }. Now let f be a function from A to subsets of B. Then R is a relation, because it is a subset of A x B. Q.E.D. If you are a programmer, you can represent a relation as a hash of hashes! Theorems and clever programs Docsity.com
Realize when you can substitute thinking for code. Remember that in the previous lecture, I substituted: (scalar (keys %domain) == scalar (keys %range)); $injective = For the brute force approach:
$injective = &TRUE; %used = %range; # keep track of what is used foreach $x (keys %f) { $image = $f{$x}; if (defined $used{$image}) { # not used yet delete $used{$image}; # mark it used } else { $injective=&FALSE; # something used twice last; # exit loop! } } Why could I do that: Theorem: Let f be a function. If f is surjective (onto) and the size of the domain and range of f are the same, then f is injective (one-to-one) (and thus bijective). Proof: Let f be surjective and let |dom(f)|=|rng(f)|. By the Being a clever programmer Docsity.com