Re: Removing an element from an array
by Zaxo (Archbishop) on Apr 28, 2004 at 02:40 UTC
|
splice.
Duplicated elements can be removed by using them as hash keys,
@hash{@array) = ();
@array = keys %hash;
or by sorting and testing successive elements. If order is important, you must decide whether you want the first or last occurrance. Then you can construct a second array by pushing on elements which haven't been seen yet,
$hash{$_}++ or push @unique, $_ for @array;
| [reply] [d/l] [select] |
Re: Removing an element from an array
by jdporter (Paladin) on Apr 28, 2004 at 03:57 UTC
|
| [reply] |
Re: Removing an element from an array
by Gerard (Pilgrim) on Apr 28, 2004 at 02:30 UTC
|
shift() and pop() might be what you are after. There is documentation for these on this site. Have you tried searching for "remove element from array" In the search box, top left of this page? I found some good suggestions there. | [reply] |
Re: Removing an element from an array
by dragonchild (Archbishop) on Apr 28, 2004 at 03:08 UTC
|
You might also be looking for splice. shift and pop (and their friends unshift and push) can be implemented in terms of splice (though they aren't actually implemented as such in the Perl source code).
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
| [reply] |
Re: Removing an element from an array
by pbeckingham (Parson) on Apr 28, 2004 at 02:36 UTC
|
#! /usr/bin/perl -w
use strict;
my @arr = (1, 2, 2, 3);
@arr = keys %{{map {$_ => undef} @arr}};
print "$_\n" for @arr;
| [reply] [d/l] |
Re: Removing an element from an array
by japhy (Canon) on Apr 28, 2004 at 07:59 UTC
|
For an effortless solution, consider Tie::Array::Unique (I fixed the link), by me.
_____________________________________________________
Jeff [japhy]Pinyan:
Perl,
regex,
and perl
hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
| [reply] |
|
|
Pardon my pointing, but that was an effortless reply.
First, the link is wrong. Throwing something in square brackets isn't magic. Try
[cpan://Tie::Array::Unique].
Second, he just said "remove", not "remove duplicates".
Now, join me in a caffeine hit, and we'll call it a morning :)
Update: Sorry, Japhy, you're right about the duplicates. I went and had that caffeine, and it seems to have helped.
Cheers,
-QM
--
Quantum Mechanics: The dreams stuff is made of
| [reply] |
|
|
First, I had "cpan://" at first, and then thought it looked wrong, so I removed the "//". I did check to make sure the link to my home node was correct, but I didn't check the CPAN link.
Second, I was providing a solution to the OP's second request: I'm sure there's some library function to remove a given element from an array, I just don't know the name. Anybody else know? And what about a function to remove duplicates from an array?
Given the fact that I posted that node at 4:00am EDT, I'm not sweating it.
_____________________________________________________
Jeff [japhy]Pinyan:
Perl,
regex,
and perl
hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
| [reply] |