References and nested data structures

13

Click here to load reader

description

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

Transcript of References and nested data structures

Page 1: References and nested data structures

Perl Programming Perl Programming CourseCourse

References and nested References and nested data structuresdata structures

Krassimir Berov

I-can.eu

Page 2: References and nested data structures

ContentsContents

1.1. What are references?What are references?

2.2. Reference typesReference types

3.3. Creating referencesCreating references

4.4. Using referencesUsing references

5.5. Reference interpolationReference interpolation

Page 3: References and nested data structures

What are references?What are references?

• A piece of data that tells us the location A piece of data that tells us the location of another piece of dataof another piece of data

• A reference is a scalar value that refers to A reference is a scalar value that refers to an array or a hash an array or a hash (or to just about anything else).(or to just about anything else).

• References are the way to implement References are the way to implement complicated data structures in Perlcomplicated data structures in Perl

• References are like pointers that know References are like pointers that know what they point towhat they point to

Page 4: References and nested data structures

Reference typesReference types

• Builtin types include:Builtin types include: SCALAR SCALAR ARRAY ARRAY HASH HASH CODE CODE REF REF GLOB GLOB LVALUE LVALUE FORMAT FORMAT IO IO VSTRING VSTRING Regexp Regexp

Page 5: References and nested data structures

Reference typesReference types

• Most used:Most used: SCALAR – reference to scalar SCALAR – reference to scalar ARRAY – reference to array ARRAY – reference to array HASH – reference to hash HASH – reference to hash CODE – reference to anonymous sub CODE – reference to anonymous sub

Page 6: References and nested data structures

Creating referencesCreating references

• Just two ways to make a referenceJust two ways to make a reference

1.1. By putting a By putting a \\ in front of a variable in front of a variable

2.2. [ITEMS][ITEMS] makes a new, anonymous array, makes a new, anonymous array, {ITEMS}{ITEMS} makes a new, anonymous hash makes a new, anonymous hash

$aref = \@array; # $aref - reference to @array$aref = \@array; # $aref - reference to @array$href = \%hash; # $href - reference to %hash$href = \%hash; # $href - reference to %hash$sref = \$scalar; # $sref - reference to $scalar$sref = \$scalar; # $sref - reference to $scalar

$aref = [ 1, "foo", undef, 13 ];$aref = [ 1, "foo", undef, 13 ];# $aref now holds a reference to an array# $aref now holds a reference to an array

$href = { APR => 4, AUG => 8 };$href = { APR => 4, AUG => 8 };# $href now holds a reference to a hash# $href now holds a reference to a hash

Page 7: References and nested data structures

Using referencesUsing references

• Just two ways to use referencesJust two ways to use references

1.1. Use curly braces preceded with the sigil Use curly braces preceded with the sigil of the variable you refer toof the variable you refer to

@{$aref}; # An array@{$aref}; # An arrayreverse @{$aref}; # Reverse the arrayreverse @{$aref}; # Reverse the array${$aref}[3]; # An element of the array${$aref}[3]; # An element of the array${$aref}[3] = 17; # Assigning an element${$aref}[3] = 17; # Assigning an element

%{$href}; # A hash%{$href}; # A hashkeys %{$href}; # Get the keys from the hashkeys %{$href}; # Get the keys from the hash${$href}{'red'} # An element of the hash${$href}{'red'} # An element of the hash${$href}{'red'} = 17; # Assigning an element${$href}{'red'} = 17; # Assigning an element

Page 8: References and nested data structures

Using referencesUsing references

• Just two ways to use referencesJust two ways to use references2.2. Use the arrow operator to access Use the arrow operator to access

elementselements● In between two subscripts, the arrow is optional.In between two subscripts, the arrow is optional.

@$aref; # An array@$aref; # An arrayreverse @$aref; # Reverse the arrayreverse @$aref; # Reverse the array$aref->[3]; # An element of the array$aref->[3]; # An element of the array$aref->[3] = 17; # Assigning an element$aref->[3] = 17; # Assigning an element$aref->[0]->[2] can be written as $aref->[0][2]$aref->[0]->[2] can be written as $aref->[0][2]%$href; # A hash%$href; # A hashkeys %$href; # Get the keys from the hashkeys %$href; # Get the keys from the hash$href->{'red'} # An element of the hash$href->{'red'} # An element of the hash$href->{'red'} = 17; # Assigning an element$href->{'red'} = 17; # Assigning an element$href->{'green'}->{leaf} $href->{'green'}->{leaf} can be written as $href->{'green'}{leaf}can be written as $href->{'green'}{leaf}

(2)

Page 9: References and nested data structures

Using referencesUsing references

• Example:Example:my $self = {my $self = { name =>'Krassi',name =>'Krassi', family_name => 'Berov',family_name => 'Berov', children =>[qw|Maria Pavel Viktoria|],children =>[qw|Maria Pavel Viktoria|],

favorite_things =>{favorite_things =>{ }}};};print "Hello! I am $self->{name} $self->{family_name}"' print "Hello! I am $self->{name} $self->{family_name}"' print 'I have '. scalar @{$self->{children}} print 'I have '. scalar @{$self->{children}} .' children.';.' children.';print 'They are named:'print 'They are named:'

. map {$_ . "\n"} @{$self->{children}};. map {$_ . "\n"} @{$self->{children}};

#see using.pl for more#see using.pl for more

Page 10: References and nested data structures

Using referencesUsing references

• Example:Example:my $self = {my $self = { name =>'Krassi',name =>'Krassi', family_name => 'Berov',family_name => 'Berov', children =>[qw|Maria Pavel Viktoria|],children =>[qw|Maria Pavel Viktoria|],

favorite_things =>{favorite_things =>{ }}};};print "Hello! I am $self->{name} $self->{family_name}"' print "Hello! I am $self->{name} $self->{family_name}"' print 'I have '. scalar @{$self->{children}} print 'I have '. scalar @{$self->{children}} .' children.';.' children.';print 'They are named:'print 'They are named:'

. map {$_ . "\n"} @{$self->{children}};. map {$_ . "\n"} @{$self->{children}};

#see using.pl for more#see using.pl for more

Page 11: References and nested data structures

Reference interpolationReference interpolation

• Example:Example:my @pets = qw|Goldy Amelia Jako|;my @pets = qw|Goldy Amelia Jako|;my $self = {my $self = { name =>'Krassi',name =>'Krassi', family_name => 'Berov',family_name => 'Berov', can => sub { return shift },can => sub { return shift }, pets => \@pets,pets => \@pets, children =>[qw|Maria Pavel Victoria|],children =>[qw|Maria Pavel Victoria|],};};

print <<TXT;print <<TXT; Hello! Hello! I am $self->{name} $self->{family_name}.I am $self->{name} $self->{family_name}. I can ${\$self->{can}('talk')}.I can ${\$self->{can}('talk')}. My first child is $self->{children}[0].My first child is $self->{children}[0]. My last child is $self->{children}[-1].My last child is $self->{children}[-1]. I do not have a pet named $self->{pets}[1].I do not have a pet named $self->{pets}[1].TXTTXT

Page 12: References and nested data structures

References References and nested data structuresand nested data structures

• ResourcesResources

• perlreftutperlreftut

• perllolperllol

• perldscperldsc

• perlrefperlref

• Beginning Perl (Chapter 7 – References)Beginning Perl (Chapter 7 – References)

Page 13: References and nested data structures

References References and nested data structuresand nested data structures

Questions?Questions?