sophate has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
pop and shift can remove an element from an array at the end or from the beginning. Is there any efficient way to remove an array element in the middle? I'm doing this with the codes below. But just wondering if there is any better way of doing this? ("delete" only sets the element to undef but doesn't remove it from the array)
#!/usr/bin/perl -w use warnings; use strict; my @FullArray=qw(1 2 3 4 5 6); print "The number of elements: [",scalar(@FullArray),"]\n"; my $LastElement = $#FullArray; print "Removing the 3rd element . . .\n"; my @Temp = @FullArray[0..1]; @FullArray = (@Temp, @FullArray[3..$LastElement]); print "The number of elements: [",scalar(@FullArray),"]\n"; print "The remaining elements:[@FullArray]\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: An efficient way to remove an element from an array
by Corion (Patriarch) on Aug 06, 2012 at 10:28 UTC | |
by Anonymous Monk on Aug 06, 2012 at 10:32 UTC | |
by Corion (Patriarch) on Aug 06, 2012 at 10:35 UTC | |
by sophate (Beadle) on Aug 06, 2012 at 10:34 UTC | |
|
Re: An efficient way to remove an element from an array
by AnomalousMonk (Archbishop) on Aug 06, 2012 at 19:10 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |