Solved

deleting elements from an array

Posted on 2000-04-04
3
193 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
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script 4 111
perl search and replace 6 171
Perl regex to replace any capital letters not preceded by ">" 6 138
Call Shell Script from Perl Script 6 134
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

828 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