0

I would like to delete a key from a hash of array reference:

if the "key" which I want to delete is 'Test', I tried something like

$Test = 'Test';

foreach my $k (keys %{$line}) {
     @{$line->{$k}} = grep @{$line->{'$Test'}} != 0, @{$line->{$k}};
}

But I could not remove it! Could someone tell me how to remove it?

1
  • It's not obvious how $line lokks like and what your plan exactly is. You should write a complete example, preferably with sample output. Commented Aug 1, 2023 at 7:57

1 Answer 1

2

The perl delete operation does this. See the perlfunc manual:

delete EXPR

Given an expression that specifies a hash element, array element, hash slice, or array slice, deletes the specified element(s) from the hash or array. In the case of an array, if the array elements happen to be at the end, the size of the array will shrink to the highest element that tests true for exists() (or 0 if no such element exists).

It is discussed here:

Sign up to request clarification or add additional context in comments.

2 Comments

I tried: for my $k (keys %{$line}) { if ($k =~ m/$Test/){delete @{$line->{$Test}};}else{next;}} But did not work. I got delete argument is not a HASH or ARRAY element or slice at ...
The delete would be applied to something like ${$line}{$k}, since that is the hash name/value I understood you were deleting. The @ indicates an array variable, while %{$line} is a hash variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.