Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Checking if an item exists in an array before adding a record to it.

by Laurent_R (Parson)
on Aug 10, 2013 at 19:15 UTC ( #1048931=note: print w/ replies, xml ) Need Help??


in reply to Checking if an item exists in an array before adding a record to it.

If the order in which you are populating the array is important, then populate both an array and an %already_seen hash (don't populate the array if the element exists in the hash). This will be much faster than greping your array each time.

If the order of insertion is not important, then don't use an array, use only a hash, you are guaranteed against duplicates. You can still sort your hash on keys.

Well, if the order is important, you might still use only a hash, with the elements as keys, and the order of insertion as values. At then end, you only need to sort the hash on the values. This will probably not be faster than populating both an array and a hash, but if speed is not crucial (no huge data set), the code will be a bit simpler.


Comment on Re: Checking if an item exists in an array before adding a record to it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048931]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-12-29 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (187 votes), past polls