How to dereference hash references

后端 未结 3 738
难免孤独
难免孤独 2021-01-26 00:05

UPDATE: Everything I know about referencing/dereferencing came from here: http://www.thegeekstuff.com/2010/06/perl-array-reference-examples/

I\'m working with a library

3条回答
  •  日久生厌
    2021-01-26 01:01

    • We have a %hash.
    • To this, we have a hash reference $hashref = \%hash
    • This hashref is inside an array @array = ($hashref)
    • We get an array reference: $arrayref = \@array

    This is our result: $result = $arrayref. Not let's work backwards:

    • @result_array = @$result
    • $result_hashref = $result_array[0] or combined: $result_hashref = $result->[0]
    • %result_hash = %$result_hashref or combined: %result_hash = %{ $result->[0] } (note that these make a copy)
    • To get an element in the result hash, we can do
      • $result_hash{$key}
      • $result_hashref->{$key}
      • $result->[0]{$key}

    Note that $scalar = @{ $arrayref } does not work as you expect, because an array (which @{ … } is) returns the length of the array in scalar context. To force list context in an assignment, put the left hand side into parens: (…) = here_is_list_context (where is a list of zero or more lvalues).

提交回复
热议问题