Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

easy way to add element to array
Hi. I'm working with an array that i'm pushing values on to, but I don't
want there to be any duplicates. Is there a perl function of an easy way to
check if an array already contains a value before I push that value on to
it?

Thanks,
Brian


Report this thread to moderator Post Follow-up to this message
Old Post
Brian Barto
10-29-04 08:56 PM


RE: easy way to add element to array
brian.barto@spectrum-health.org wrote:
> Hi. I'm working with an array that i'm pushing values on to, but I
> don't want there to be any duplicates. Is there a perl function of an
> easy way to check if an array already contains a value before I push
> that value on to it?

First consider whether you should be using a hash.

If you still want to use an array, the Tie::File::Array module will do the
trick.


Report this thread to moderator Post Follow-up to this message
Old Post
Bob Showalter
10-29-04 08:56 PM


RE: easy way to add element to array
Bob Showalter wrote:
> If you still want to use an array, the Tie::File::Array module will
> do the trick.

Duh, that should be "Tie::Array::Unique". Sorry 'bout that.

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Showalter
10-29-04 08:56 PM


RE: easy way to add element to array
Though about using a hash but I don't have a need for a Key/Value
combination. I just need a list without duplicates. I suppose I can define
the hash keys if they don't already exists and export the keys into an array
with the keys function. Is that what you are hinting towards?

Thanks,
Brian

-----Original Message-----
From: Bob Showalter [mailto:Bob_Showalter@taylorwhite.com]
Sent: Friday, October 29, 2004 2:56 PM
To: 'brian.barto@spectrum-health.org'; beginners@perl.org
Subject: RE: easy way to add element to array


brian.barto@spectrum-health.org wrote:
> Hi. I'm working with an array that i'm pushing values on to, but I
> don't want there to be any duplicates. Is there a perl function of an
> easy way to check if an array already contains a value before I push
> that value on to it?

First consider whether you should be using a hash.

If you still want to use an array, the Tie::File::Array module will do the
trick.

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Barto
10-30-04 01:55 AM


RE: easy way to add element to array
brian.barto@spectrum-health.org wrote:
> Though about using a hash but I don't have a need for a Key/Value
> combination. I just need a list without duplicates. I suppose I can
> define the hash keys if they don't already exists and export the keys
> into an array with the keys function. Is that what you are hinting
> towards?

Yes, that's what I'm hinting at :~)

A hash would be called for if you need to do repeated tests to see whether
an element exists in the set. The downside to pulling the unique values with
keys() is that you lose the original order, if that matters.

Tie::Array::Unique will take care of the problem and preserve the order,
while letting you push entries onto the array without worrying about
creating duplicates.

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Showalter
10-30-04 01:55 AM


Re: easy way to add element to array
Also look at
perldoc -q duplicate
and
Perl Cookbook 4.6. Extracting Unique Elements from a List

"Brian Barto" <brian.barto@spectrum-health.org> wrote in message ...
> Though about using a hash but I don't have a need for a Key/Value
> combination. I just need a list without duplicates. I suppose I can define
> the hash keys if they don't already exists and export the keys into an
array
> with the keys function. Is that what you are hinting towards?



Report this thread to moderator Post Follow-up to this message
Old Post
Zeus Odin
10-30-04 08:55 AM


RE: easy way to add element to array
On Oct 29, Bob Showalter said:

>brian.barto@spectrum-health.org wrote: 
>
>Yes, that's what I'm hinting at :~)
>
>A hash would be called for if you need to do repeated tests to see whether
>an element exists in the set. The downside to pulling the unique values wit
h
>keys() is that you lose the original order, if that matters.
>
>Tie::Array::Unique will take care of the problem and preserve the order,
>while letting you push entries onto the array without worrying about
>creating duplicates.

Thank you for the plug. :)

Tie::Array::Unique is a transparent solution -- that is, all you do is
tie the array to T::A::U and things get done.  Using hashes isn't all that
hard, but T::A::U does that for you.

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart


Report this thread to moderator Post Follow-up to this message
Old Post
Jeff 'japhy' Pinyan
10-30-04 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:19 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.