Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!

I am sending all the DATA to be processed by process() sub, since I am expecting a lot of data I want to process 100 rows at a time.
The issue I have is that its giving me this error:

Not a HASH reference at... this line: my $name = $row->{NAME};
I don’t understand!!!

Thanks for looking!
... sub process { my $arrayref = shift; while ( my @data = splice( @{ $arrayref }, 0, 100 ) ) { foreach my $row (@data) { my $name = $row->{NAME}; my $year = $row->{YEAR}; print "$name and $address\n"; } } } __DATA__ $VAR1 = [ { 'NAME' => 'LAST PAUL', 'YEAR' => '2000', }, { 'NAME' => 'DEMARCO R', 'YEAR' => '1960', }, { 'NAME' => 'JOE M', 'YEAR' => '1981', }, { 'NAME' => 'LOU MARY', 'YEAR' => '1948', }, ];

Replies are listed 'Best First'.
Re: Not a HASH reference
by 1nickt (Canon) on Jul 07, 2015 at 18:05 UTC

    It was hard to see what was going on because you obviously patched that code together rather than pasting directly. your problem has to do with how you are creating your arrayref ... the code in your sub works fine:

    #!/usr/bin/perl -w use strict; my $arrayref = [ { 'NAME' => 'LAST PAUL', 'YEAR' => '2000', }, { 'NAME' => 'DEMARCO R', 'YEAR' => '1960', }, { 'NAME' => 'JOE M', 'YEAR' => '1981', }, { 'NAME' => 'LOU MARY', 'YEAR' => '1948', }, ]; while ( my @data = splice( @{ $arrayref }, 0, 100 ) ) { foreach my $row (@data) { my $name = $row->{NAME}; my $year = $row->{YEAR}; print "$name and $year\n"; } }
    [11:15][nick:~/dev/perl_dev/test/monks]$ perl 1133585.pl LAST PAUL and 2000 DEMARCO R and 1960 JOE M and 1981 LOU MARY and 1948
    Remember: Ne dederis in spiritu molere illegitimi!
Re: Not a HASH reference
by stevieb (Canon) on Jul 07, 2015 at 18:19 UTC

    How are you calling process(), and with what data? Your code works fine for me when I rename $VAR1 in the Dumper output to $aref, rename $address to $name and call like this: process($aref).

    Could it be that one or more elements in your data aren't hash references?

    foreach my $row (@data){ if (ref($row) ne 'HASH'){ print "$row not a href\n"; next; } }

    -stevieb