Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perl Variable Types

ynonperek
November 06, 2011

Perl Variable Types

A roundup of variable types in perl and their usage

ynonperek

November 06, 2011
Tweet

More Decks by ynonperek

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. ✦ 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
  4. Arrays ✦ Ordered collections of scalars ✦ Dynamically grow or

    shrink ✦ Marked in perl with a @ Sunday, November 6, 2011
  5. Arrays 1 3 5 1 3 7 2 @numbers =

    (1, 3, 5, 1, 3, 7, 2); Sunday, November 6, 2011
  6. 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
  7. 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
  8. 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
  9. 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
  10. Array Splicing 1 3 5 1 3 7 2 splice

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

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

    3 5 7 2 splice @a, 3, 2; Sunday, November 6, 2011
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. ✦ Elements are accessed by index ✦ Cannot identify an

    element by a meaningful name ✦ Works best for cloned masses Sunday, November 6, 2011
  28. Hashes ✦ A collection of named scalars ✦ Each scalar

    is accessible via a unique name ✦ Easy store/retrieve elements Sunday, November 6, 2011
  29. 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
  30. Hash Key Value Paris 1 in 1 the 2 spring

    1 Sunday, November 6, 2011
  31. 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
  32. 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
  33. 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
  34. 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