7. Perl ABC Data Structure
Special Things – Nested List
There is NOT anythig like list of lists
#+begin_src perl
@a = qw(fred barney betty wilma);
@b = ("tom", @a, "jerry"); # be careful
# what you actually get is
@b = qw(tom fred barney betty wilma jerry);
#+end_src
8. Perl ABC Data Structure
Special Things – Nested List
But … there is nested list in the real world
What you really mean is
#+begin_src perl
@a = qw(fred barney betty wilma);
@b = ("tom", [ @a ], "jerry");
@b = ("tom", @a, "jerry");
#+end_src
#+begin_src perl
$c = [ @a ]; $c = @a;
@b = ("tom", $c, "jerry");
#+end_src
9. Perl ABC Data Structure
Special Things – Nested Hash
There is nested hash in the real world
#+begin_src perl
$words_nest{ mash } = {
captain => "pierce",
%words_nest = (
major => "burns",
fred => "camel",
barney => "llama", corporal => "radar",
};
betty => "alpaca",
wilma => "alpaca",
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy", # Key quotes needed.
},
);
#+end_src
10. Perl ABC Data Access
Data Access
Access Scalar Data
Access List Data
Access Hash Data
11. Perl ABC Data Access
Data Access
Access Scalar Data
Access List Data
Access Hash Data
12. Perl ABC Data Access
Scalar
$fred = "pay"; $fredday = "wrong!";
$barney = "It's $fredday";
# not payday, but "It's wrong!"
$barney = "It's ${fred}day";
# now, $barney gets "It's payday"
$barney2 = "It's $fred"."day";
# another way to do it
$barney3 = "It's " . $fred . "day";
# and another way
13. Perl ABC Data Access
Data Access
Access Scalar Data
Access List Data
Access Hash Data
16. Perl ABC Data Access
List – access as a whole
foreach
map
grep
17. Perl ABC Data Access
List – access as a whole
foreach
#+begin_src perl
@a = (3,5,7,9);
foreach $one (@a) {
$one *= 3;
}
# @a is now (9,15,21,27)
Notice how altering $one in fact altered each element of @a.
This is a feature, not a bug.
18. Perl ABC Data Access
List – access as a whole
map
@a = (3,5,7,9);
@b = map { $_ * 3 } @a; # @b is now (9,15,21,27)
@c = map { $_ > 5 } @a; # @c is now (,1,1)
grep
@a = (3,5,7,9);
@c = grep { $_ > 5 } @a; # @c is now (7,9)
@c = grep { $_ > 5 ? $_ : ()} @a; # equivalent as map
19. Perl ABC Data Access
List – access as a whole
map and equivalent foreach
@a = (3,5,7,9);
@b = map { $_ * 3 } @a; # @b is now (9,15,21,27)
# equivalent foreach
foreach my $a (@a) {
push @b, $a * 3; # did not return values
}
20. Perl ABC Data Access
List – access as a whole
sub time3 {
map and equivalent foreach
my $num = shift;
@a = (3,5,7,9); return $num * 3
}
@b = map { $_ * 3 } @a;
$func = sub {
my $num = shift;
return $num * 3
}
# equivalents sub my_map {
@b = map &time3($_) @a; my ($func, $data) = @_;
@b = map &$func($_) @a; foreach $a (@$data) {
@b = my_map &time3, @a; push @b, &$func($a);
@b = my_map $func, @a; }
return @b;
}
24. Perl ABC Data Access
Hash – access nested elements
%h_nest = (
fred => "camel",
barney => "llama",
betty => "alpaca",
wilma => "alpaca",
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy",
},
);
$c = $h_nest{"jetsons"}{"wife"}; # $c = jane
$j = "jetsons"; $w = "wife";
$c = $h_nest{$j}{$w}; # same thing
$jet = $h_nest("jetsons"}; # $jet has a hash
$d = $jet{"husband"}; # $d = george
25. Perl ABC Data Access
Reference
# Create some variables
$a = "mama mia";
@array = (10, 20);
%hash = ("laurel" => "hardy", "nick" => "nora");
# Now create references to them
$r_a = $a; # $ra now "refers" to (points to) $a
$r_array = @array;
$r_hash = %hash;
26. Perl ABC Data Access
Access Reference Data
# Now create references to them
$r_a = $a; # $ra now "refers" to (points to) $a
$r_array = @array;
$r_hash = %hash;
# Now access the referenced data
$r_a; # the address
$$r_a; # the $a; "mama mia";
$r_array # the address
@$r_array # the array (10, 20);
@$r_array[1] # the element 20;
27. Perl ABC Data Access
Access Reference Data
# Now create references to them
$r_a = $a; # $ra now "refers" to (points to) $a
$r_array = @array;
$r_hash = %hash;
# Now access the referenced data
$r_a; # the address
$$r_a; # the $a; "mama mia";
$r_array # the address
@$r_array # the array (10, 20);
@$r_array[1] # the element 20;