Assigning multiple hash entries with deref chain in front

Discussion in 'Perl Misc' started by Konrad Eisele, May 6, 2005.

  1. Assigning multiple hash with one statement is done with:
    @a{'a','b'} = (1,2);
    but how can I do it if I want to access the hash through
    previous dereferences:
    ---------------------------
    use Data::Dumper;
    @a = ({'a'=>1,'b'=>2});
    $b = \@a;
    $b->[0]->{'a','b'} = (100,1000);
    print Dumper(\@a);
    ---------------------------

    $b->[0]->{'a','b'} = (1,2) doesn't work here.

    How can I force list context?

    -- Konrad
     
    Konrad Eisele, May 6, 2005
    #1
    1. Advertising

  2. (Konrad Eisele) wrote in
    news::

    > Assigning multiple hash with one statement is done with:
    > @a{'a','b'} = (1,2);


    Assuming %a is a hash, that is referred to as, @a{'a','b'} is a hash
    slice.

    > but how can I do it if I want to access the hash through
    > previous dereferences:
    > ---------------------------


    use strict;
    use warnings;

    > use Data::Dumper;
    > @a = ({'a'=>1,'b'=>2});


    @a now is an array containing a single element which is a reference to
    an anonymous hash. BTW, you don't need to quote a and b above, that is
    done automatically by using => for the keys you have chosen in this
    case.

    > $b = \@a;


    $b now contains a reference to \@a. (Incidentally, $b is a package
    variable used by sort, so it is not a good idea to use it for other
    purposes).

    > $b->[0]->{'a','b'} = (100,1000);


    Well, $b->[0] is the reference to the hash you created above, so you
    need to get a hash slice out of that first:

    use strict;
    use warnings;

    my @x = ( { a => 1, b => 2 } );
    my $y = \@x;

    @{ %{$y->[0]} }{ qw(a b) } = (100, 1000);

    use Data::Dumper;
    print Dumper(\@x);

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 6, 2005
    #2
    1. Advertising

  3. Konrad Eisele <> wrote:
    > Assigning multiple hash with one statement is done with:
    > @a{'a','b'} = (1,2);



    A "hash slice".


    > but how can I do it if I want to access the hash through
    > previous dereferences:
    > ---------------------------
    > use Data::Dumper;
    > @a = ({'a'=>1,'b'=>2});
    > $b = \@a;
    > $b->[0]->{'a','b'} = (100,1000);
    > print Dumper(\@a);
    > ---------------------------



    > How can I force list context?



    You cannot force list context, but that doesn't matter because
    your question is not related to context in any way.

    Your real question is:

    How do I dereference a hashref?

    You can do that by applying "Use Rule 1" from

    perldoc perlreftut


    I like to do it in 3 steps.

    Pretend you are slicing a plain-ol' hash:

    @hash{'a','b'} = (100,1000);

    Replace the _name_ with a block:

    @{ }{'a','b'} = (100,1000);

    Fill in the block with something that returns the right type of reference:

    @{ $b->[0] }{'a','b'} = (100,1000);


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 6, 2005
    #3
  4. A. Sinan Unur <> wrote:

    > @{ %{$y->[0]} }{ qw(a b) } = (100, 1000);

    ^^ ^
    ^^ ^

    Delete those 3 characters.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 6, 2005
    #4
  5. Tad McClellan <> wrote in
    news::

    > A. Sinan Unur <> wrote:
    >
    >> @{ %{$y->[0]} }{ qw(a b) } = (100, 1000);

    > ^^ ^
    > ^^ ^
    > Delete those 3 characters.


    Done:

    @{ $y->[0] }{ qw(a b) } = (100, 1000);

    :)

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 6, 2005
    #5
    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. John Doe

    deref in a string into an operand

    John Doe, Nov 5, 2004, in forum: Python
    Replies:
    4
    Views:
    276
    Peter Abel
    Nov 5, 2004
  2. Don Bruder
    Replies:
    3
    Views:
    978
    spikeysnack
    Aug 3, 2010
  3. rp
    Replies:
    1
    Views:
    540
    red floyd
    Nov 10, 2011
  4. Patrick
    Replies:
    5
    Views:
    595
    Brian McCauley
    May 3, 2006
  5. Lee

    Help - Inline::C arrayref deref?

    Lee, Jun 9, 2006, in forum: Perl Misc
    Replies:
    15
    Views:
    159
    Anno Siegel
    Jun 24, 2006
Loading...

Share This Page