Hash/array reference question

Discussion in 'Perl Misc' started by Bryan, Apr 6, 2007.

  1. Bryan

    Bryan Guest

    If I have a hash reference, and want to assign a reference to a snapshot
    of a changing array, what is the correct syntax?

    $hash_ref->{'key'} = @array;
    Obviously doesn't work

    $hash_ref->{'key'} = \@array;
    Doesn't work since my array is changed then reused for different keys,
    the values get hosed.

    my @tmp_array = @array;
    $hash_ref->{'key'} = \@tmp_array;
    Works as I want, but now I have an extra line of code.

    Is that really necessary? Seems a bit clumsy to have to make a local
    copy explicitly like that...

    Thanks,
    B
     
    Bryan, Apr 6, 2007
    #1
    1. Advertising

  2. Bryan

    Paul Lalli Guest

    On Apr 6, 12:47 pm, Bryan <> wrote:
    > If I have a hash reference, and want to assign a reference to a snapshot
    > of a changing array, what is the correct syntax?
    >
    > $hash_ref->{'key'} = @array;
    > Obviously doesn't work
    >
    > $hash_ref->{'key'} = \@array;
    > Doesn't work since my array is changed then reused for different keys,
    > the values get hosed.
    >
    > my @tmp_array = @array;
    > $hash_ref->{'key'} = \@tmp_array;
    > Works as I want, but now I have an extra line of code.
    >
    > Is that really necessary? Seems a bit clumsy to have to make a
    > local copy explicitly like that...


    Create a reference to a new anonymous array, and populate that array
    with the current values of @array:

    $hash_ref->{key} = [ @array ];

    Paul Lalli
     
    Paul Lalli, Apr 6, 2007
    #2
    1. Advertising

  3. Bryan

    Bryan Guest

    Paul Lalli wrote:
    > On Apr 6, 12:47 pm, Bryan <> wrote:
    >> If I have a hash reference, and want to assign a reference to a snapshot
    >> of a changing array, what is the correct syntax?
    >>
    >> $hash_ref->{'key'} = @array;
    >> Obviously doesn't work
    >>
    >> $hash_ref->{'key'} = \@array;
    >> Doesn't work since my array is changed then reused for different keys,
    >> the values get hosed.
    >>
    >> my @tmp_array = @array;
    >> $hash_ref->{'key'} = \@tmp_array;
    >> Works as I want, but now I have an extra line of code.
    >>
    >> Is that really necessary? Seems a bit clumsy to have to make a
    >> local copy explicitly like that...

    >
    > Create a reference to a new anonymous array, and populate that array
    > with the current values of @array:
    >
    > $hash_ref->{key} = [ @array ];
    >
    > Paul Lalli
    >


    Ah, cool, thanks.

    B
     
    Bryan, Apr 6, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    275
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    235
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
  5. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    120
    Steve
    Sep 28, 2003
Loading...

Share This Page