slayedbylucifer
slayedbylucifer

Reputation: 23502

Perl Grep issue in nested hash/array

I want to remove values which contain "local" string in their value. here is my hash output (print Dumper ($hash)):

$VAR1 = {
          'FARM_03' => [
                           'nfs01',
                           'nfs02',
                           'nfs03',
                           'localvmfs',
                           'localvmfs'
                           ],
          'FARM_07' => [
                           'nfs01',
                           'localvmfs',
                           'localvmfs'
                           ],
          'FARM_11' => [
                           'nfs01',
                           'localvmfs',
                           'localvmfs'
                           ]
        };

Hence I wrote below code in my script to omit the "local" entries:

foreach my $key ( keys %$hash )
{
    @{ $hash->{key} } = grep { !/local/i } @{ $hash->{key} };
}

and here is the output after running above grep command:

$VAR1 = {
          'FARM_03' => [
                           'nfs01',
                           'nfs02',
                           'nfs03',
                           'localvmfs',
                           'localvmfs'
                           ],
          'FARM_07' => [
                           'nfs01',
                           'localvmfs',
                           'localvmfs'
                           ],
          'FARM_11' => [
                           'nfs01',
                           'localvmfs',
                           'localvmfs'
                           ]
          'key' => []
    };

It is not removing the "local" entries as well as it adds a new field 'key' => [].

Could you tell me what is wrong with my grep statement.

Thanks.

Upvotes: 0

Views: 899

Answers (1)

ysth
ysth

Reputation: 98398

You have {key} where you mean {$key} (twice).

Upvotes: 4

Related Questions