I have an Array of Hashes as below.
@students= (
{
'math' => 95,
'phy' => 90,
'che' => 85
},
{
'math' => 50,
'phy' => 70,
'che' => 35
}
);
I want to delete a entire hash based on some conditions, for that i tried with below code but am getting an error saying delete argument is not a HASH or ARRAY element or slice
. So please help me, how can i do?
for $i ( 0 .. $#students) {
for $key ( keys %{ $students[$i] } ) {
if ($key eq 'che') {
if ($students->{$key} == 35){
delete (%{$students[$i]});
}
}
}
}
Deleting is well suited for hash keys, but in your case you want to remove array elements so grep
filtering could be applied:
@students = grep { $_->{che} != 35 } @students;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With