Now I'm guessing what you want to do is kind of like this:

my @peeps = qw( Martin Sharon Rikke ); my %ages; $ages{'Martin'} = 44; $ages{'Rikke'} = 12; $ages{'Sharon'} = 23; my @insert = @ages{@peeps}; # @insert = ( 44, 23, 12 )

What you're asking to do is take references to the elements of %ages and store them in @insert so that you can modify @insert via %ages. Maybe what you really want is aliases, but you seem to be asking for this...

my @peeps = qw( Martin Sharon Rikke ); my %ages; @ages{@peeps} = (); # make keys with undef values # take references to the values my @insert = \@ages{@peeps}; # same thing but more explicit $insert[0] = \$ages{'Martin'}; $insert[1] = \$ages{'Sharon'}; $insert[2] = \$ages{'Rikke'}; # same thing but shorter push @insert, \$ages{$_} for @peeps; $ages{'Martin'} = 44; $ages{'Rikke'} = 12; $ages{'Sharon'} = 23; my @ages_array = map { ${$_} } @ages{@peeps};

In that case, each element of @insert is a scalar reference to some value in %ages. To get (or modify) those values via @insert, you have to dereference them (e.g., ${$insert[1]}), so that's not exactly what you're asking for either.

For more on references, see perlreftut, perlref, and References quick reference.


In reply to Re: array/hash - reference and dereference by kyle
in thread array/hash - reference and dereference by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.