190 likes | 466 Views
References and Nested Structures. Overview of References. References are scalar variables that store addresses. Called pointers in other languages. Address can be of: Named variables. Anonymous variables. Even sub-programs. Why?. Why do we need references?
E N D
Overview of References • References are scalar variables that store addresses. • Called pointers in other languages. • Address can be of: • Named variables. • Anonymous variables. • Even sub-programs.
Why? • Why do we need references? • Arrays and hashes are not enough. • Some applications require more complex data structures. • These structures can be defined and manipulated with references.
References • (\) gets the address of a named variable. • The address can be assigned to a scalar variable. • Example: $sum = 0; $ref_sum = \$sum; @list = (1, 3, 5, 7); $ref_list = \@list; • $ref_sum and $ref_list have the address of $sum and @list.
Graphical View Memory 0 1 2 $ref_sum 0 1000 1001 1002 1 3 $ref_list
More References • References can also point to literals (i.e. actual values) and functions. • [] gets the address of a list literal. • e.g. $ref_list = [1, 3, 5, 7]; • {} gets the address of a hash literal. • e.g. $ref_hash = {‘Bob’ => 30, ‘Ted’ => 40}; • \ get the address of a scalar literal. • e.g. $ref_pi = \3.14;
More References (cont.) • An array variable can be placed in a []. • e.g. @a = (1, 2, 3); $ref_array = [ @a ]; • Creates a copy of @a and assigns address of copy to $ref_array. • {} can be used for hash variables. • e.g. $ref_hash = { %hash };
What’s the difference • foreach $i (0 .. 10) { $temp = <STDIN>; @a = split / /, $temp; $sent[$i] = \@a; } • foreach $i (0 .. 10) { $temp = <STDIN>; @a = split / /, $temp; $sent[$i] = [ @a ]; }
Dereference • A reference has two associated values: • The stored address. • The value at the address. • The process of getting the value at the address is called dereferencing. • Using an undereferenced variable manipulates the address value. • All dereferences are explicit.
Dereferencing Variables • Dereference a scalar variable by putting a ($) in front of the variable’s name. • Example: $sum = 17; $ref_sum = \$sum; print “The sum is $$ref_sum \n”;
Dereferencing Variables (cont.) • Put @ in front of variables referencing arrays. • Example: @a = (1, 2, 3, 4); $ref_array = \@a; print “@$ref_array \n”; • Put % in front of variables referencing hashes. • e.g. $ref_hash→ %$ref_hash
Getting Array and Hash Elements • (->) gets elements of an array or hash reference. • (->) placed between variable name and subscript. • Example: $ref_list = \@array; $ref_hash = \%hash; $ref_list -> [3] = 17; $ref_hash -> {‘Bob’} = 42; • Can also use: • $$ref_list[3] = 17; • $$ref_hash{‘Bob’} = 42;
Undefined References • What happens to $$x if $x is not a reference? • Example: $sum = 17; $str = ‘sum’; print “$$str \n”; • This is called a soft reference. • Can be very dangerous.
Nested Structures • References can be used to build complex structures: • Multi-dimensional array. • Arrays of hashes. • Hashes of hashes. • And much more.
2D Arrays • 2D arrays can be implemented as an Array of References to Arrays (ARA). • Example: @mat = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] ); • @mat is not itself a reference! • If rows already exist, then • @mat = ( \@r1, \@r2, \@r3 );
2D Arrays (cont.) • Can also be a Reference to an Array of References to Arrays (RARA). • Example: $ref_mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ];
Accessing the Elements • Use (->) and subscripts for an ARA: • $mat[2] -> [3] • $mat[2][3] • For a RARA: • $ref_mat -> [2][3] • $$ref_mat[2][3] • There is an implied (->) between subscripts.
Properties of Multi-D Arrays • An ARA is not interchangeable with a RARA. • There is no distinction in C. • Each row can be of any length. • Entire array can grow as needed.
Array of Hashes • Another useful complex structure: @students = ( {name => “Bob”, grade => “A”}, {name => “Fred”, grade => “B”}, {name => “Mary”, grade => “A”} ); • Elements can be accessed by: • $students[$index] -> {“name”} • $students[$index]{“name”}