Below is the code
my $results = $session->array_of_hash_for_cursor("check_if_receipts_exist", 0, @params);
next if( scalar @{$results} <= 0 );
$logger->info("Retrieved number of records: ".scalar @$results);
foreach my $row( sort { $results->{$b}->{epoch_received_date} cmp $results->{$a}->{epoch_received_date} } keys %{$results} )
{
//logic
}
‘check_if_receipts_exists’ is a SQL query which returns some results. Which I try to execute this, I am getting the following error,
Bad index while coercing array into hash
I am new to Perl. Can someone please point out the mistake I am making?
Is
$resultsa hash reference or an array reference?In some places you are using it like an array reference:
and in other places you are using it like a hash reference:
It can’t be both (at least not without some heavy
overloadmagic).If I can infer from the name of the function that sets
$results, it should be a reference to a list of hash references, then a few tweaks will set it right:@{$results}is correct; this expression is “an array of hash references”sortshould be a list, but the correct list to pass is@{$results}, notkeys %{$results}.$aand$binside thesortfunction will be members of@{$results}, that is, they will be hash references. So the comparison to make isAll together: