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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |