Re: A ref too far: assign an array ref to an array ref to a hash element

From: John W. Krahn (krahnj_at_acm.org)
Date: 05/29/04

  • Next message: Andrew Gaffney: "Re: regex help"
    To: beginners@perl.org
    Date: Fri, 28 May 2004 15:44:22 -0700
    
    

    Angie Ahl wrote:
    >
    > Hi people

    Hello,

    > I'm trying to create a hash of arrays of arrays and I'm having a mind
    > twisting time with references.
    >
    > my $key = "some_varying_text"
    > my %pathhash;
    > my @link = ($LinkUrl, $LinkTitle);
    >
    > I'm trying to set $pathhash{$key} to an array that contains the array
    > @link.
    > ie there will be multiple @link arrays in $pathhash{$key}.
    >
    > I just can't work out how to assign an array ref to an array ref to a
    > hash element. I think that's right.
    >
    > Any clues anyone... please, 4 hours down and a lot of RTFM'ing hasn't
    > helped (quite the opposite actually;)

    If @link is lexically scoped then use a reference:

    $pathhash{ $key }[ 0 ] = \@link;
    # or
    push @{ $pathhash{ $key } }, \@link;

    If not then you have to copy it to an anonymous array:

    $pathhash{ $key }[ 0 ] = [ @link ];
    # or
    push @{ $pathhash{ $key } }, [ @link ];

    John

    -- 
    use Perl;
    program
    fulfillment
    

  • Next message: Andrew Gaffney: "Re: regex help"

    Relevant Pages

    • Re: Test if memory pointer is valid?
      ... I can see setting a new pointer initially to the address of the beginning of an array, for example, and then incrementing the pointer to address subsequent elements, but that would usually be done inside a function, where the new pointer would go out of scope at the end of the function and be reinitialized if it is called again. ... I would like to see a specific example where it is necessary to have multiple references such as this where it would be necessary to check for it having been freed. ... copies of all the TLabel references already in the form as individual ...
      (comp.lang.pascal.delphi.misc)
    • Re: UBound not in Intellisense. References problem?
      ... but you can produce an array by using Value ... I wouldn't worry about it not appearing in intellisense. ... > I wonder if I have a corruption in my registration of References, ... I get UCase, UCase$, but no UBound. ...
      (microsoft.public.excel.programming)
    • Re: How come Ada isnt more popular?
      ... The language does not require array implementation to be contiguous. ... then But non contigous representation of arrays will really stress ... It's easy with value-oriented languages (i.e. languages ... use references because of that. ...
      (comp.lang.ada)
    • Re: Reclaiming Memory
      ... example, handles to device context, memory addresses, etc.), VB is actually ... VB will clean up references to objects all ... It's a little unusual to use an array to store object references. ... unusual to use a variant array for what you apparently are (at least I'VE ...
      (microsoft.public.vb.com)
    • Re: subroutines, prototyping, and pass by reference question
      ... > There are three main reasons to use references: ... > sub process_string { ... > #we have now copied our 8 million character string ... > There is no way to create, for example, a two-dimensional array without ...
      (comp.lang.perl.misc)