References and nested data structures

919 views
666 views

Published on

This is the eighth set of slightly updated slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at i-can.eu.
The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
919
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

References and nested data structures

  1. 1. Perl Programming Course References and nested data structuresKrassimir BerovI-can.eu
  2. 2. Contents1. What are references?2. Reference types3. Creating references4. Using references5. Reference interpolation
  3. 3. What are references?• A piece of data that tells us the location of another piece of data• A reference is a scalar value that refers to an array or a hash (or to just about anything else).• References are the way to implement complicated data structures in Perl• References are like pointers that know what they point to
  4. 4. Reference types• Builtin types include: SCALAR ARRAY HASH CODE REF GLOB LVALUE FORMAT IO VSTRING Regexp
  5. 5. Reference types• Most used: SCALAR – reference to scalar ARRAY – reference to array HASH – reference to hash CODE – reference to anonymous sub
  6. 6. Creating references• Just two ways to make a reference 1. By putting a in front of a variable $aref = @array; # $aref - reference to @array $href = %hash; # $href - reference to %hash $sref = $scalar; # $sref - reference to $scalar 2. [ITEMS] makes a new, anonymous array, {ITEMS} makes a new, anonymous hash $aref = [ 1, "foo", undef, 13 ]; # $aref now holds a reference to an array $href = { APR => 4, AUG => 8 }; # $href now holds a reference to a hash
  7. 7. Using references• Just two ways to use references 1. Use curly braces preceded with the sigil of the variable you refer to @{$aref}; # An array reverse @{$aref}; # Reverse the array ${$aref}[3]; # An element of the array ${$aref}[3] = 17; # Assigning an element %{$href}; # A hash keys %{$href}; # Get the keys from the hash ${$href}{red} # An element of the hash ${$href}{red} = 17; # Assigning an element
  8. 8. Using references (2)• Just two ways to use references 2. Use the arrow operator to access elements ● In between two subscripts, the arrow is optional. @$aref; # An array reverse @$aref; # Reverse the array $aref->[3]; # An element of the array $aref->[3] = 17; # Assigning an element $aref->[0]->[2] can be written as $aref->[0][2] %$href; # A hash keys %$href; # Get the keys from the hash $href->{red} # An element of the hash $href->{red} = 17; # Assigning an element $href->{green}->{leaf} can be written as $href->{green}{leaf}
  9. 9. Using references• Example: my $self = { name =>Krassi, family_name => Berov, children =>[qw|Maria Pavel Viktoria|], favorite_things =>{ } }; print "Hello! I am $self->{name} $self->{family_name}" print I have . scalar @{$self->{children}} . children.; print They are named: . map {$_ . "n"} @{$self->{children}}; #see using.pl for more
  10. 10. Using references• Example: my $self = { name =>Krassi, family_name => Berov, children =>[qw|Maria Pavel Viktoria|], favorite_things =>{ } }; print "Hello! I am $self->{name} $self->{family_name}" print I have . scalar @{$self->{children}} . children.; print They are named: . map {$_ . "n"} @{$self->{children}}; #see using.pl for more
  11. 11. Reference interpolation• Example: my @pets = qw|Goldy Amelia Jako|; my $self = { name =>Krassi, family_name => Berov, can => sub { return shift }, pets => @pets, children =>[qw|Maria Pavel Victoria|], }; print <<TXT; Hello! I am $self->{name} $self->{family_name}. I can ${$self->{can}(talk)}. My first child is $self->{children}[0]. My last child is $self->{children}[-1]. I do not have a pet named $self->{pets}[1]. TXT
  12. 12. References and nested data structures• Resources • perlreftut • perllol • perldsc • perlref • Beginning Perl (Chapter 7 – References)
  13. 13. Referencesand nested data structuresQuestions?

×