EpiMan
EpiMan

Reputation: 839

how to delete key from hash of array reference in perl

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?

Upvotes: 0

Views: 3047

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54465

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:

Upvotes: 2

Related Questions