Link to home
Create AccountLog in
Avatar of ckaspar
ckaspar

asked on

grep an array

I need to split @array2 on the /:/ and if  array2[1] matches any element in @array1 remove it from @array1


@array1 = qw/xxxxx.xml yyyyy.xml ttttt.xml eeeee.xml/;
@array2 = qw/DELETE:xxxxx.xml DELETE:ttttt.xml /;
my @field;
 
  foreach (@array2) {
    @field = split/:/;
        grep if array2[1] is in @array1 remove it from@array1
        or
        something close to this

FINAL ouput would be @array1 = qw/yyyyy.xml ttttt.xml/;  
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I think you had a little typo :
FINAL ouput would be @array1 = qw/yyyyy.xml eeeee.xml/;

foreach (@array2) {
    @field = split/:/;
        #grep if array2[1] is in @array1 remove it from@array1
        #or
        #something close to this :
        @array1=grep/!$field[1]/@array1;
}

if you want to KNOW prel, understand OZO's solution,
I posted somthing you asked.
tal
Avatar of jbtux
jbtux

#!/usr/bin/perl -w
use strict;


my @array1 = qw/xxxxx.xml yyyyy.xml ttttt.xml eeeee.xml/;
my @array2 = qw/DELETE:xxxxx.xml DELETE:ttttt.xml /;
my @ar_mod = map {split /:/ } @array2;
my %hash = map { $_ => 1} @array1;

 foreach my $elem (@ar_mod) {
  delete $hash{$elem} if exists $hash{$elem};
  }

might do what you want.

/jbt