On Aug 15, 2006, at 2:02 PM, SFantar wrote:
Hello everyone
I would like to sort an array so that it does not contain any
values twice or three times.
Unfortunately, perldoc -q sort does not help me very much.
Here is an example of this array :
@array = ('smith ', 'john ', 'edward ', 'smith ', 'edward ', 'john ');
I would like to have : @array1 = ('smith ', 'john ', 'edward ');
Thanks in advance for your help and especially your explanations.
From the example looks like you want to filter out duplicates but
preserve the relative order of elements in the original array. A
possible way to accomplish that is:
feynman% cat ~/tmp/foo.pl
@array = ('smith ', 'john ', 'edward ', 'smith ', 'edward ', 'john ');
%seen = ();
@uniq = grep { $seen{$_} = !exists $seen{$_} } @array;
print "@uniq\n";
feynman% perl ~/tmp/foo.pl
smith john edward
The code in the block for grep is a bit dense, it can be made more
explicit if needed.
-- fxn
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>