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/;
@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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
#!/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
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
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]/@a
}
if you want to KNOW prel, understand OZO's solution,
I posted somthing you asked.
tal