Solved

deleting elements from an array

Posted on 2000-04-04
3
167 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
3 Comments
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 50 total points
Comment Utility
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
Comment Utility
Thanks.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
#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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now