Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

delete an element from an array?

1 view
Skip to first unread message

Bing Du Test

unread,
May 14, 2003, 11:46:26 AM5/14/03
to
I know how to delete an element from a hash. What about deleting from
an array if I just know the element value rather than its position in
the array? I've looked 'delete' up in perlfunc, but still have no clear
clue.

Thanks in advance for any help,

Bing

Helgi Briem

unread,
May 14, 2003, 12:11:10 PM5/14/03
to
On Wed, 14 May 2003 10:46:26 -0500, Bing Du Test
<bin...@tamu.edu> wrote:

>I know how to delete an element from a hash. What about deleting from
>an array if I just know the element value rather than its position in
>the array? I've looked 'delete' up in perlfunc, but still have no clear
>clue.

perldoc -f grep

#!perl
use warnings;
use strict;
my @array = qw/one two three/;
my $exclude = 'two';
@array = grep !/^$exclude$/,@array;
print join "\n",@array;
__END__
--
Regards, Helgi Briem
helgi DOT briem AT decode DOT is

Brian McCauley

unread,
May 14, 2003, 12:28:17 PM5/14/03
to
Bing Du Test <bin...@tamu.edu> writes:

> I know how to delete an element from a hash. What about deleting from
> an array if I just know the element value rather than its position in
> the array?

It probably means you should have been using a hash not an array.

Even if you are convinced you don't want a hash, are you sure you
really need to delete from the _array_?

Would replacing the contents of the array with a new list that was
equal to the old contents but without the element be sufficient?

@a = grep { $_ ne $e } @a;

This isn't deleting from the array - but for most purposes the
difference is not significant.

> I've looked 'delete' up in perlfunc, but still have no clear
> clue.

Well it tells you - use splice()

If you really want to delete from the array:

for ( reverse $#a .. 0 ) {
splice @a, $_, 1 if $a[$_] eq $e;
}

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\

Brian McCauley

unread,
May 14, 2003, 12:30:26 PM5/14/03
to
he...@decode.is (Helgi Briem) writes:

> @array = grep !/^$exclude$/,@array;

@array = grep !/^\Q$exclude\E$/,@array;

Of course more natural is:

@array = grep $_ ne $exclude,@array;

Bill Smith

unread,
May 14, 2003, 6:55:47 PM5/14/03
to

"Helgi Briem" <he...@decode.is> wrote in message
news:3ec269b4....@news.cis.dfn.de...

> On Wed, 14 May 2003 10:46:26 -0500, Bing Du Test
> <bin...@tamu.edu> wrote:
>
> >I know how to delete an element from a hash. What about deleting
from
> >an array if I just know the element value rather than its position in
> >the array? I've looked 'delete' up in perlfunc, but still have no
clear
> >clue.
>
> perldoc -f grep
>
> #!perl
> use warnings;
> use strict;
> my @array = qw/one two three/;
> my $exclude = 'two';
> @array = grep !/^$exclude$/,@array;
> print join "\n",@array;
> __END__
> --

It is not clear to me what the OP means by 'delete from an array'. Your
interpretation is probably correct, but he may want to undefined the
value and leave the indices unchanged.


Purl Gurl

unread,
May 14, 2003, 7:34:25 PM5/14/03
to
Bing Du Test wrote:

(snipped)

> What about deleting from an array if I just know the element value rather
> than its position in the array?

You will discover this method beneath my signature
to be noticeably more efficient than grep for almost
all circumstances.

Double the size of my array, and this method is twice
as efficient as grep. As array size grows, my method
becomes almost exponentially quicker than grep.

If an element to be deleted is near the beginning of
an array, even more efficient. Towards the end of an
array, my method and grep start approaching equality
in efficiency but my method is still more efficient.

Grep is ok for very small arrays. Grep is a poor choice
for large arrays. Should you elect to use grep, investigate
your options pertaining to efficiency. Nothing wrong with
grep for many circumstances but there is often a better
programming choice to be made.

This method is designed for single element deletion but
can be modified for duplicate element deletion with ease.


Purl Gurl
--
http://www.purlgurl.net


TEST SCRIPT:
____________

#!perl

print "Content-type: text/plain\n\n";

@Array = qw (Purl Gurl Rocks OhOh And Rolls!);
for (@Array)
{
if ($_ eq "OhOh")
{ splice (@Array, $count, 1); last; }
$count++;
}

print "@Array";


PRINTED RESULTS:
________________

Purl Gurl Rocks And Rolls!

0 new messages