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

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?

Replies are listed 'Best First'.
Re^3: Put references to array of hash elements in an array
by choroba (Cardinal) on Aug 03, 2018 at 06:51 UTC
    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

        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,
Re^3: Put references to array of hash elements in an array
by LanX (Saint) on Aug 03, 2018 at 09:58 UTC
    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.
        It's a "hack" because you really need special reasons to assign to a list of references, that is rarely needed and complicated.

        It looks like you are updating a full record of ages from SQL.

        I'd suggest to look into DBI->fetchall_hashref() and even considering changing your data structure to ->fetchall_hashref("name") (or whatever your primary key is)

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