Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messagebrian.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.
Post Follow-up to this messageBob 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.
Post Follow-up to this messageThough 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.
Post Follow-up to this messagebrian.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.
Post Follow-up to this messageAlso 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?
Post Follow-up to this messageOn 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.