Solved

deleting elements from an array

Posted on 2000-04-04
3
201 Views
Last Modified: 2012-05-04
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
Comment
Question by:leoleo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 50 total points
ID: 2686491
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
 

Author Comment

by:leoleo
ID: 2686786
Thanks.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2687314
#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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question