### Re^3: An irrational coding choice found

by BrowserUk (Pope)
 on Mar 21, 2012 at 16:54 UTC ( #960816=note: print w/replies, xml ) Need Help??

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

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?

• Comment on Re^3: An irrational coding choice found

Create A New User
Node Status?
node history
Node Type: note [id://960816]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2018-06-22 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (124 votes). Check out past polls.

Notices?