How to remove matching elements from the array? Let say I have array @A [1, 2, 2, 3, 4, 4, 4, 5] and now I have remove element 2 and 3 so I should see @A [ 1, 4, 4, 4, 5] only in the array.
Asked
Active
Viewed 400 times
-1
mpapec
- 49,466
- 8
- 63
- 119
user1595858
- 3,426
- 12
- 60
- 102
-
http://search.cpan.org/perldoc?List%3A%3AUniq – Kevin Panko Nov 26 '13 at 01:48
1 Answers
4
You can use grep to filter out elements you don't want:
my @A = (1, 2, 2, 3, 4, 4, 4, 5);
my @newA = grep { $_ != 2 } @A;
# @newA has elements (1,3,4,4,4,5)
Hunter McMillen
- 56,682
- 21
- 115
- 164
-
Can I store the modified output in the same array like @A = grep {$_ !=2 } @A ? – user1595858 Nov 25 '13 at 20:31