Slide 1

Slide 1 text

Variable Types $ % @ Sunday, November 6, 2011

Slide 2

Slide 2 text

Scalar Types ✦ A scalar is a two-face single value ✦ Its value can be read as a string or as numeric, depending on the context ✦ Scalars are the most basic type in perl Sunday, November 6, 2011

Slide 3

Slide 3 text

Variable Prefix ✦ Each perl variable name has a sigil prefix ✦ For scalars, the sigil is $. The $ sign functions like the word ‘the’ in english. Sunday, November 6, 2011

Slide 4

Slide 4 text

Scalars Are Broken Sunday, November 6, 2011

Slide 5

Slide 5 text

✦ A scalar represents a single value ✦ Need a way to represent multiple values, such as class grades ✦ Arrays & Hashes to the rescue Sunday, November 6, 2011

Slide 6

Slide 6 text

Ordered Collections Arrays Sunday, November 6, 2011

Slide 7

Slide 7 text

Arrays ✦ Ordered collections of scalars ✦ Dynamically grow or shrink ✦ Marked in perl with a @ Sunday, November 6, 2011

Slide 8

Slide 8 text

Arrays 1 3 5 1 3 7 2 @numbers = (1, 3, 5, 1, 3, 7, 2); Sunday, November 6, 2011

Slide 9

Slide 9 text

Array Creation ✦ Init an array by assigning a list to it ✦ Can use (...) or qw/.../ to define the list ✦ Can initialize an array from another array my @a = (2, 3, 5); my @b = (@a, 10, 20); my @colors = qw/red blue/; my @names = (‘Jim’, ‘Jane’); my @c = (@a, 10, @b); Sunday, November 6, 2011

Slide 10

Slide 10 text

Array Manipulations ✦ Copy an array to another array using assignment operator ✦ Copy parts of array using array slices ✦ Slice sizes don’t need to be equal my @a = (2, 3, 5); my @b = (10, 20); my @c = (11, 13, 15); @a = @b; @c[2, 3] = @a[1, 2]; @c[1..10] = (‘t’) x 2; Sunday, November 6, 2011

Slide 11

Slide 11 text

Array Functions ✦ push adds an element at the end of the array ✦ pop removes the last element of the array my @a = (2, 3, 5); my @b = (10, 20); my @c = (11, 13, 15); push @a, 10; print pop @a; # prints 10 print pop @a; # prints 5 Sunday, November 6, 2011

Slide 12

Slide 12 text

Array Functions ✦ unshift inserts an element at the beginning of an array ✦ shift removes the first element from the array my @a = (2, 3, 5); my @b = (10, 20); my @c = (11, 13, 15); unshift @a, 10; print shift @a; # prints 10 print shift @a; # prints 2 Sunday, November 6, 2011

Slide 13

Slide 13 text

Array Splicing 1 3 5 1 3 7 2 splice @a, 3, 2; Sunday, November 6, 2011

Slide 14

Slide 14 text

Array Splicing 1 3 5 1 3 7 2 1 3 5 1 3 7 2 splice @a, 3, 2; Sunday, November 6, 2011

Slide 15

Slide 15 text

Array Splicing 1 3 5 1 3 7 2 1 3 5 7 2 splice @a, 3, 2; Sunday, November 6, 2011

Slide 16

Slide 16 text

Array Functions ✦ splice removes a subset of the array ✦ splice takes offset and length, deletes the array slice, and changes all subsequent indeces ✦ Careful using in the middle of a for loop Sunday, November 6, 2011

Slide 17

Slide 17 text

Using Lists ✦ A list is a collection of scalars inside parens ✦ Assigning to a list will assign to each of the scalars my @a = (2, 3, 5); my @b = (10, 20); my @c = (11, 13, 15); my ($x, $y) = @a; Sunday, November 6, 2011

Slide 18

Slide 18 text

Context ✦ Reading a scalar as a list yields a list with a single element ✦ Reading a list as a scalar yields the list size ✦ Use scalar function to force scalar context ✦ Use (...) to force list contexts ✦ Some functions will work differently based on the context Sunday, November 6, 2011

Slide 19

Slide 19 text

Context Examples my @a = (1, 2, 3); my $x = 5; my @b = $x; # now @b is (5) $x = @a; # now $x == 3 print @a; # prints the list print scalar @a; #prints its size if ( @a > 5 ) { print “There are more than 5 elements\n”; } Sunday, November 6, 2011

Slide 20

Slide 20 text

Context Captcha my $text = ‘Take me to your leader’; my $rev = reverse $text; print $text, “\n”; print reverse $text, “\n”; print reverse($text), “\n”; print $rev, “\n”; Sunday, November 6, 2011

Slide 21

Slide 21 text

Scalar Or List Name the context of each colored expression my $x = f(); Name the context of each colored expression my ($x) = f(); Name the context of each colored expression my @x = f(); Sunday, November 6, 2011

Slide 22

Slide 22 text

Scalar Or List Name the context of each colored expression my $x = f(); Name the context of each colored expression my ($x) = f(); Name the context of each colored expression my @x = f(); Scalar List List Sunday, November 6, 2011

Slide 23

Slide 23 text

Scalar Or List Name the context of each colored expression print f(); Name the context of each colored expression print int(f()); Name the context of each colored expression print scalar(f()); Sunday, November 6, 2011

Slide 24

Slide 24 text

Scalar Or List Name the context of each colored expression print f(); Name the context of each colored expression print int(f()); Name the context of each colored expression print scalar(f()); List Scalar Scalar Sunday, November 6, 2011

Slide 25

Slide 25 text

Scalar Or List Name the context of each colored expression push f(), 10; Name the context of each colored expression my $t = f() > 2; Name the context of each colored expression print reverse f(); Sunday, November 6, 2011

Slide 26

Slide 26 text

Scalar Or List Name the context of each colored expression push f(), 10; Name the context of each colored expression my $t = f() > 2; Name the context of each colored expression print reverse f(); Error Scalar List Sunday, November 6, 2011

Slide 27

Slide 27 text

Lists Vs. Arrays ✦ In most cases they are equal. Except when they’re not ✦ The following assigns ‘c’ to $foo: my $foo = (‘a’, ‘b’, ‘c’); Sunday, November 6, 2011

Slide 28

Slide 28 text

Array Subscripts 1 3 5 7 2 Sunday, November 6, 2011

Slide 29

Slide 29 text

Access By Index ✦ Use $array[$idx] to access an element by its index ✦ Note the $. It’s like saying “The third student”, which is singular ✦ Subscripts can be negative my @a = (2, 3, 5); print $a[2]; # prints 5 print $a[0]; # prints 2 print $a[-1]; #prints 5 Sunday, November 6, 2011

Slide 30

Slide 30 text

Exercise ✦ Write a perl that reads lines of input, and writes the number of words in each line (hint: perldoc -f split) ✦ Write a perl that calculates the 7th element in a fibonacci series (http://en.wikipedia.org/wiki/ Fibonacci_number) Sunday, November 6, 2011

Slide 31

Slide 31 text

Arrays Are Broken Sunday, November 6, 2011

Slide 32

Slide 32 text

✦ Elements are accessed by index ✦ Cannot identify an element by a meaningful name ✦ Works best for cloned masses Sunday, November 6, 2011

Slide 33

Slide 33 text

Sometimes A name is part of the deal Sunday, November 6, 2011

Slide 34

Slide 34 text

Hashes ✦ A collection of named scalars ✦ Each scalar is accessible via a unique name ✦ Easy store/retrieve elements Sunday, November 6, 2011

Slide 35

Slide 35 text

Example ✦ Write a perl that counts word frequency in a given text ✦ For the input: “Paris in the the spring” ✦ Program should print: Paris - 1, in - 1, the - 2, spring - 1 Sunday, November 6, 2011

Slide 36

Slide 36 text

Hash Key Value Paris 1 in 1 the 2 spring 1 Sunday, November 6, 2011

Slide 37

Slide 37 text

Hash Syntax ✦ Initialize a hash with a list. ✦ Elements in the even indices are considered keys ✦ Elements in the odd indices are considered values my %shapes = ( triangle => 3, rectangle => 4, ); Sunday, November 6, 2011

Slide 38

Slide 38 text

Hash Access ✦ Access a hash record using curly braces ✦ Assign to an existing key to overwrite the value ✦ Assign to a non existing key to add a new value my %bus_line = ( 42 => ‘nice’, 24 => ‘fast’, 18 => ‘efficient’, 49 => ‘slow’, ); $bus_line{42} = ‘happy’; $bus_line{3} = ‘clean’; print $bus_line{18}; Sunday, November 6, 2011

Slide 39

Slide 39 text

Hash Functions ✦ exists checks if a key exists in the hash ✦ delete removes a value from the hash using a key my %bus_line = ( 42 => ‘nice’, 24 => ‘fast’, 18 => ‘efficient’, 49 => ‘slow’, ); if ( exists $bus_line{42} ) { print “42 is here\n”; } delete $bus_line{18}; Sunday, November 6, 2011

Slide 40

Slide 40 text

Hash Iteration while ( my ($line, $desc) = each %bus_line ) { print “$line is $desc\n”; } foreach my $line (keys %bus_line) { print “$line is “, $bus_line{$line}; } foreach my $desc (values %bus_line) { print “a bus is $desc\n”; } Sunday, November 6, 2011

Slide 41

Slide 41 text

Q & A Sunday, November 6, 2011

Slide 42

Slide 42 text

Thank You ✦ Ynon Perek ✦ [email protected] ✦ ynonperek.com Sunday, November 6, 2011