References - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

References

Description:

References – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 11
Provided by: PaulL155
Category:
Tags: hiya | references

less

Transcript and Presenter's Notes

Title: References


1
References
2
References
  • Analagous (somewhat) to pointers in C/C
  • Far less messy, and definitely less dangerous
  • Assign a reference to a variable to a scalar
    variable.
  • Use the \ to create a reference
  • _at_foo (1, 2, 3)
  • aref \_at_foo
  • aref now contains a reference to the array _at_foo
  • Changes to _at_foo will affect array referenced by
    aref
  • Changes to the array referenced by aref will
    affect _at_foo

3
De-Referencing
  • De-reference a reference using the appropriate
    variable symbol (_at_ for array, for hash, etc)
  • aref \_at_foo
  • _at_new_array _at_aref
  • contents of a simple scalar ? can drop them
    _at_aref
  • _at_aref can be used like any other array
  • push _at_aref, 'val1', 'val2'
  • _at_new_array is a different array, which contains
    the same values of members that the array
    referenced by aref contained.
  • same as if you'd done _at_new_array _at_foo
  • Changes to _at_foo or aref do NOT affect _at_new_array
  • Changes to _at_aref DO affect _at_foo

4
Referencing Other Types
  • You can also reference other kinds of variables
  • hash('Paul'gt26, 'Gavin'gt20)
  • h \hash
  • bar "hello world\n"
  • bar_ref \bar

5
Anonymous References
  • A value need not be contained in a defined
    variable to create a reference.
  • To create an anonymous array reference
  • use square brackets, instead of parens
  • arr 20, 30, 50, "hi!!"
  • _at_a _at_arr
  • _at_a ? (20, 30, 50, "hi!!")
  • For hash references, use curly brackets, instead
    of parens
  • hsh'sky'gt'blue','grass'gt'green'
  • h hsh
  • h ? ('sky' gt 'blue', 'grass' gt 'green')
  • To de-reference specific element of references.

6
TMTOWTDI
  • a_ref 'Hi', 'Hiya', 'Hello'
  • _at_a_ref ('Hi', 'Hiya', 'Hello')
  • a_ref2 'Hello'
  • a_ref2 'Hello'
  • a_ref-gt2 'Hello'
  • h_refkey value
  • h_refkey value
  • h_ref-gtkey value
  • These are all valid and acceptable. The form you
    choose is whatever looks the best to you.

7
Why bother?
  • Weeks ago, we briefly mentioned two-dimensional
    arrays
  • The mention was "Don't Do That."
  • To create a 2D array, create an array of array
    references
  • _at_two_d (1, 2, 3, 4, 5, 6)
  • two_d1 is a reference to an array containing
    (3, 4)
  • _at_two_d1 is an array containing (3, 4)
  • two_d10 is the scalar value 3.
  • two_d1-gt0 is the same thing
  • Any time an arrow separates two brackets ( or
    ), it can be dropped completely
  • two_d10 is also the scalar value 3.
  • Other arrows CANNOT be dropped
  • foo-gt10 is NOT equivalent to foo10

8
More Complicated
  • Using similar methods, you can create arrays of
    hashes, hashes of arrays, hashes of hashes,
    arrays of arrays of hashes, hashes of hashes of
    arrays, arrays of hashes of arrays, . . . . .
  • letters ( lower gt 'a' .. 'z', upper gt
    'A' .. 'Z')
  • letterslower is an array reference
  • _at_letterslower is an array
  • letterslower1 is the scalar value 'b'.
  • letterslower-gt1 is the scalar value 'b'
  • letterslower1 is the scalar value 'b'

9
Schwartzian Transform
  • Randal Schwartz devised a method to efficiently
    sort a list based on a computation-intense
    calculation
  • To sort a list of files based on file size
  • sort -s a ltgt -s b _at_files
  • The stat has to be done twice every time two
    files are compared. That's costly!
  • _at_sorted map _-gt0sort a-gt1 ltgt
    b-gt1map _, -s _ _at_files
  • Now stat is done only once for each filename

10
Help available
  • perldoc perlreftut
  • tutorial on references
  • perldoc perllol
  • "lists of lists" very inaccurate
  • perldoc perldsc
  • Data Structures Cookbook building complicated
    structures
  • perldoc perlref
  • Reference reference.
Write a Comment
User Comments (0)
About PowerShow.com