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

This way:
my @data; foreach my $rdata ( @{ $sqldata } ) { push @data, [ $rdata->{'NAME'}, $rdata->{'HOME'}, ... ]; }

Replies are listed 'Best First'.
Re^7: Adding an element to an array
by poj (Abbot) on Oct 15, 2015 at 17:43 UTC

    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
      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
      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
      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]}; }