• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

deleting elements from an array

I have an array like this:
@arrAlph = ('wer', 'abc', 'xyz', ... 'abc', ..);

I want to delete all the 'abc' s from it. They maybe at any position.
Is there an easy method?

0
leoleo
Asked:
leoleo
1 Solution
 
Kim RyanIT ConsultantCommented:
You can use grep to filter out all the 'abc's and assign redult to a new array.

@arrAlph = ('wer', 'abc', 'xyz', 'fds', 'abc', 'dds');
@newAlph = grep(!/abc/,@arrAlph);
# copy back to original array if needed
@arrAlph = @newAlph;
print(@arrAlph);

0
 
leoleoAuthor Commented:
Thanks.
0
 
ozoCommented:
#You can assign to the same array
@arrAlph = grep !/abc/,@arrAlph;
#but that will also elimitate elements like 'xyzabcdef'
#if you only want to eliminate 'abc'
@arrAlph = grep $_ ne 'abc',@arrAlph;
#or
@arrAlph = grep {$_ ne 'abc'} @arrAlph;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now