thanks!

what is the fastest way if i have 100000 items?

-----Original Message-----
From: Oron Peled [mailto:[EMAIL PROTECTED]
Sent: Monday, October 08, 2007 11:11 PM
To: [email protected]
Cc: Ernst, Yehuda
Subject: Re: [Israel.pm] exists item in array


On Monday, 8 בOctober 2007, Ernst, Yehuda wrote:
> I want to enter items to array only if they does not exits there
> (i need 1 item of each type) 
> if i have     a b c d d d c
> the array will have
> a b c d

That was one of my favorites when I was teaching perl.

Several alternative solutions (with different conditions):

  @in = ('a', 'c', 'c', 'd', 'd', 'd', 'c');

1. Assume sorted items (your example), preserve order:

  my $prev = 'nosuchvalue';
  @out = grep($_ ne $prev && ((($prev) = $_),1), @in);

2. Any order input, preserve order:

  undef %saw;
  @out = grep(!$saw{$_}++, @in);

3. Any order input, does not preserve order:

  undef %a;
  @[EMAIL PROTECTED] = ();
  @out = keys(%a);

4. Any order input, @in contains (not very large) numbers,
   does not preserve order:

  @[EMAIL PROTECTED] = @in;
  @out = sort @a;

Have fun,

-- 
Oron Peled                             Voice/Fax: +972-4-8228492
[EMAIL PROTECTED]                  http://www.actcom.co.il/~oron
ICQ UIN: 16527398

"Beware of bugs in the above code;
 I have only proved it correct, not tried it."
                              -- Donald E. Knuth
*********************************************************************************************************
This e-mail is confidential, the property of NDS Ltd and intended for the 
addressee only.  Any dissemination, copying or distribution of this message or 
any attachments by anyone other than the intended recipient is strictly 
prohibited.  If you have received this message in error, please immediately 
notify the [EMAIL PROTECTED] and destroy the original message.  Messages sent 
to and from NDS may be monitored.  NDS cannot guarantee any message delivery 
method is secure or error-free.  Information could be intercepted, corrupted, 
lost, destroyed, arrive late or incomplete, or contain viruses.  We do not 
accept responsibility for any errors or omissions in this message and/or 
attachment that arise as a result of transmission.  You should carry out your 
own virus checks before opening any attachment.  Any views or opinions 
presented are solely those of the author and do not necessarily represent those 
of NDS.

NDS Limited Registered office: One Heathrow Boulevard, 286 Bath Road, West 
Drayton, Middlesex, UB7 0DQ, United Kingdom. A company registered in England 
and Wales  Registered no. 3080780   VAT no. GB 603 8808 40-00

To protect the environment please do not print this e-mail unless necessary.
**********************************************************************************************************

_______________________________________________
Perl mailing list
[email protected]
http://perl.org.il/mailman/listinfo/perl

Reply via email to