tel2 has asked for the wisdom of the Perl Monks concerning the following question:

Beloved Monks,

In this code I'm trying to get an array of references to point at elements of an array of hashes.
@AoH = ( { name => Adam, age => 0 }, { name => Bob, age => 10 }, { name => Cat, age => 20 } ); # Trying something simple first (2 scalars on the left of the assignme +nt) ($age_ref0,$age_ref2) = (\$AoH[0]{age},\$AoH[2]{age}); print "\$\$age_ref0 = $$age_ref0\n"; # Prints '$$age_ref0 = 0' print "\$\$age_ref2 = $$age_ref2\n"; # Prints '$$age_ref2 = 20' $$age_ref2 = 21; print "\$\$age_ref2 = $$age_ref2 = $AoH[2]{age}\n"; # The above prints '$$age_ref2 = 21 = 21' # The above went to plan, so but let's try to do similarly using an ar +ray on the left of the assignment @age_ref = (\$AoH[0]{age},\$AoH[2]{age}); print "\$\$age_ref[1] = $$age_ref[1] = @$age_ref[1] = $AoH[2]{age}\n"; # The above prints '$$age_ref[1] = = = 21'. Why??? print Dumper(\@age_ref); # (Essentially) prints: '\0, \21'
What am I doing wrong, please?

Thanks.
tel2

Replies are listed 'Best First'.
Re: Put references to array of hash elements in an array
by LanX (Saint) on Aug 03, 2018 at 03:11 UTC
    > What am I doing wrong, please?

    Precedence!

    $$age_ref[1] means ${$age_ref}[1] not ${$age_ref[1]}

    @AoH = ( { name => Adam, age => 0 }, { name => Bob, age => 10 }, { name => Cat, age => 20 } ); @age_ref = (\$AoH[0]{age},\$AoH[2]{age}); print "\${\$age_ref[1]} = ${$age_ref[1]} = $AoH[2]{age}\n"; # prints ${$age_ref[1]} = 20 = 20

    HTH! :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Actually I'm still having problems in my real code.

      When reading a table with DBI into @age_ref, should this syntax update the age values in @AoH?

      @age_ref = (\$AoH[0]{age},\$AoH[2]{age}); while (@age_ref = \$sth->fetchrow_array)

      The problem is, the age values in the @HoA are not being updated as a result of the above code.
      Any ideas on how to make that work, please?

        References work the other way round. \$AoH[0]{age} is a reference to the value stored under age, i.e. something like \42, it's no longer connected to the structure it came from.

        You need to store the references in the @AoH if you want it to reflect the changes:

        #!/usr/bin/perl use warnings; use strict; my @AoH; my @age_ref = (42, 33); @AoH = ( { age => \$age_ref[0] }, {}, { age => \$age_ref[1] }, ); use Data::Dumper; print Dumper \@age_ref; @age_ref = (1, 2); use Data::Dumper; print Dumper \@AoH;

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        I think you want to look into DBI's ->bind_col and ->bind_columns instead of these hacks.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Orsum!

      Thanks heaps for the speedy and useful reply, Rolf!  Feel free to take a pay rise.

        > Feel free to take a pay rise.

        Send me the scalps of three Pythonistas and we are even! ;-)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice