user3457622
user3457622

Reputation: 59

Perl: Removal element from an multiple array

I am trying to remove value from array I have tried all the below 3 methods but dumper output before and after remains same.

@array:

$VAR1 = [
          [
            'LINK-IF-A/1/1/1<->IF-B/1/1/1',
            'LINK-IF-C/1/1/1<->IF-D/1/1/1',
            'LINK-IF-E/1<->IF-F/2'
          ]
        ];

$value = LINK-IF-C/1/1/1<->IF-D/1/1/1

Method1 :

my @remove = grep { $_ != "$value" } @array;

Method2 :

my @remove = grep { grep {!/$value/ } @$_ } @array;

Method3 :

my @remove = grep(!/"$value"/, @array);

DEBUG(Dumper\@remove)); --> Same output as input ...no removal

Thanks,

Upvotes: 0

Views: 70

Answers (2)

vol7ron
vol7ron

Reputation: 42179

Codepad

One way is to loop through the array and grep each internal array:

my @remove = map { [ grep { !/$value/ } @$_ ] } @array;

If your outer array is really only an array of one element, you can avoid using map and pop the array off the stack:

my @remove =  [ grep { !/$value/ } @{$array[0]} ];

Upvotes: 0

ikegami
ikegami

Reputation: 386706

for my $inner (@array) {
   @$inner = grep { $_ eq $value } @$inner;
}

Sorry, don't have time to explain.

Upvotes: 1

Related Questions