Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: An irrational coding choice found

by raybies (Chaplain)
on Mar 21, 2012 at 14:16 UTC ( #960776=note: print w/ replies, xml ) Need Help??


in reply to Re: An irrational coding choice found
in thread An irrational coding choice found

one nitpick (probably too obvious to mention, but I'm a simpleton...), You can save time using a hash if you need to access a single element of a hash, instead of searching the array for your elements. Also hash insertion's easy, as opposed to splicing into a sorted array (assuming you had to keep an array sorted). It really comes down to how you intend to use your data as to whether it saves time.


Comment on Re^2: An irrational coding choice found
Re^3: An irrational coding choice found
by jdporter (Canon) on Mar 21, 2012 at 14:37 UTC

    New (or rather, newly identified) software pattern: Simpleton Object.

Re^3: An irrational coding choice found
by BrowserUk (Pope) on Mar 21, 2012 at 16:54 UTC
    one nitpick (probably too obvious to mention ...

    It's not so much "too obvious" as not really applicable to the OPs question.

    I wasn't trying to define the absolute criteria for when to use an array or a hash.

    Just clarify the reasoning for trading the habitual use of hashes for arrays where that is otherwise a practical proposition.

    In order for that trade to even begin to be a practical proposition, the "natural keys" to the data have to be small, near contiguous integers; or otherwise be simply and quickly transformable into a range of small, near contiguous integers. If that is not the case, no such trade is possible and the OPs question is moot.

    You can save time using a hash if you need to access a single element of a hash, instead of searching the array for your elements.

    In order to use the lookup abilities of an associative array, you must start with the key and either:

    • be looking for the associated value;
    • checking for the existence of that key.

    For the very specific subset of cases where the array-instead-of-hash trade is possible, it would mean that you would be starting with an index into the array. And either looking to retrieve the value at that index; or test if there is any value at that index.

    In both cases, if the initial trade is feasible then the lookup in the array will be substantially faster than lookup in the hash.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-09-21 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls