For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2004 > easy way to add element to array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author easy way to add element to array
Brian Barto

2004-10-29, 3:56 pm

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

Bob Showalter

2004-10-29, 3:56 pm

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.

Bob Showalter

2004-10-29, 3:56 pm

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.
Brian Barto

2004-10-29, 8:55 pm

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.
Bob Showalter

2004-10-29, 8:55 pm

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.
Zeus Odin

2004-10-30, 3:55 am

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?



Jeff 'japhy' Pinyan

2004-10-30, 3:55 am

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 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.


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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com