I have a hash like this:
%hash = ('test' => 1,
'test1' => 2,
'test2' => 3,);
I want to sort this hash and delete the first key value pair from hash. If I do this,
(sort keys %hash)[0], I get access to first key. However, how do I delete that key value pair?
If I do
delete (sort keys %hash)[0]
Perl throws an error,
delete argument is not a HASH or ARRAY element
The expression
(sort keys %hash)[0]returns a string, so you can’t just pass that todelete. You have to telldeletewhich hash you’re deleting from. It should go like this: