in reply to Re^3: Put references to array of hash elements in an array
in thread Put references to array of hash elements in an array

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

Replies are listed 'Best First'.
Re^5: Put references to array of hash elements in an array
by choroba (Cardinal) on Aug 03, 2018 at 08:40 UTC
    When you assign to @AoH directly
    @AoH = ...

    the previous contents of the @AoH is irrelevant, the array now contains different elements. When you assign to something an elements references to, it changes the thing being referenced.

    ($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 choroba,

      > "When you assing to @AoH directly (@AoH = ...) the previous contents of the @AoH is irrelevant, the array now contains different elements."

      Where in my code did I assign anything to @AoH directly via "@AoH = ..." apart from at the very beginning?

        Sorry, you assigned to @age_ref directly.
        while (@age_ref = \$sth->fetchrow_array)
        ($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,