in reply to Put references to array of hash elements in an array

> 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

Replies are listed 'Best First'.
Re^2: Put references to array of hash elements in an array
by tel2 (Pilgrim) on Aug 03, 2018 at 04:52 UTC
    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,
        Thanks for that, choroba,

        If references work the other way round (i.e. I need to store the reference in the @AoH), then why does this code update the @AoH?:

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

        Thanks.
        Tel2

      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

        I didn't know they were hacks, Rolf, but thanks for the tips.
Re^2: Put references to array of hash elements in an array
by tel2 (Pilgrim) on Aug 03, 2018 at 03:22 UTC
    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

        Sounds like a reasonable deal.  Hope we don't get caught by any Hate Speech laws.  Or we might just get called racist or languageist.