[Israel.pm] exists item in array
Oron Peled
oron at actcom.co.il
Wed Oct 10 00:50:54 EEST 2007
On Tuesday, 9 בOctober 2007, Ernst, Yehuda wrote:
> thanks!
>
> what is the fastest way if i have 100000 items?
Ahhhh, you meant you are going to benchmark it for us?
Please do so and let us know the results.
Thanks,
>
> -----Original Message-----
> From: Oron Peled [mailto:oron at actcom.co.il]
> Sent: Monday, October 08, 2007 11:11 PM
> To: perl at perl.org.il
> 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;
> @a{@in} = ();
> @out = keys(%a);
>
> 4. Any order input, @in contains (not very large) numbers,
> does not preserve order:
>
> @a[@in] = @in;
> @out = sort @a;
>
> Have fun,
>
> --
> Oron Peled Voice/Fax: +972-4-8228492
> oron at actcom.co.il 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 postmaster at nds.com 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
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>
--
Oron Peled Voice/Fax: +972-4-8228492
oron at actcom.co.il http://www.actcom.co.il/~oron
ICQ UIN: 16527398
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the universe trying to produce
bigger and better idiots. So far, the universe is winning.
-- Rick Cook, Mission Manager, NASA Mars Pathfinder Project
More information about the Perl
mailing list