in reply to Re^6: Adding an element to an array
in thread Adding an element to an array

Your error is because @data is an array of ARRAY references not HASH references. Try

my %homes_by_names=(); my @data=(); foreach my $rdata ( @{ $sqldata } ) { ++$homes_by_names{ $rdata->{'NAME'} }; push @data,$rdata; } foreach my $row ( @data ) { $row->{'HOME_COUNT'} = $homes_by_names{ $row->{ NAME } }; } print Dumper \@data;
poj

Replies are listed 'Best First'.
Re^8: Adding an element to an array
by Anonymous Monk on Oct 15, 2015 at 18:03 UTC
    I tried this way because at this point in my code @data is like this:
    [ [ "John Doe", "Main Street", "House 1", ], [ "John Doe", "Main Street", "House 1", ], ... ]
    I tried this way:
    foreach my $row ( @data) { $row = $homes_by_names{$row->[1]}; }
    But I am getting this:
    Can't use string ("2") as an ARRAY ref while "strict refs" in use at . +..
      Can't use string ("2") as an ARRAY ref while "strict refs" in use at . ??

      What is the line number in the error message and what is the code on that line ?

      poj
Re^8: Adding an element to an array
by Anonymous Monk on Oct 15, 2015 at 19:03 UTC
    Why wouldn't this work???
    for my $row (0..$#data) { push @{ $data[$row] }, $homes_by_names{$row->[1]}; }
    Can't use string ("0") as an ARRAY ref while "strict refs" in use

    The line number is where I am trying to assign the value into "@data" inside of the foreach loop
      $row is a number, its not an array ref , $row is $row its not @data
Re^8: Adding an element to an array
by Anonymous Monk on Oct 15, 2015 at 19:37 UTC
    I got it to work this way:
    for my $row (0..$#data) { # Stick all the array data in here my $all_data = $data[$row]; push @{ $data[$row] }, $homes_by_names{$all_data->[1]}; }