1 / 19

References and Nested Structures

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?

amy
Télécharger la présentation

References and Nested Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. References and Nested Structures

  2. 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.

  3. 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.

  4. 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.

  5. Graphical View Memory 0 1 2 $ref_sum 0 1000 1001 1002 1 3 $ref_list

  6. 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;

  7. 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 };

  8. 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 ]; }

  9. 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.

  10. 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”;

  11. 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

  12. 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;

  13. 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.

  14. Nested Structures • References can be used to build complex structures: • Multi-dimensional array. • Arrays of hashes. • Hashes of hashes. • And much more.

  15. 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 );

  16. 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] ];

  17. 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.

  18. 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.

  19. 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”}

More Related