in reply to array/hash - reference and dereference

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.

Replies are listed 'Best First'.
Re^2: array/hash - reference and dereference
by Anonymous Monk on Jan 09, 2009 at 05:15 UTC
    Thanks.
    take references to the elements of %ages and store them in @insert so that you can modify @insert via %ages.
    Yes, that is what I am looking for. Thank you very much. And yes, I will have problem on:
    $sqlquery->execute(@insert);
    As I have to dereference them.

    The purpose of this idea is to speed up the SQL process. But now, it seems this idea can't work.

    Again, a big thank you to all of you.