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

How are you creating @data ?
poj

Replies are listed 'Best First'.
Re^6: Adding an element to an array
by Anonymous Monk on Oct 15, 2015 at 17:09 UTC
    This way:
    my @data; foreach my $rdata ( @{ $sqldata } ) { push @data, [ $rdata->{'NAME'}, $rdata->{'HOME'}, ... ]; }

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