Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: HASH Array advice

by serf (Chaplain)
on Jan 24, 2006 at 11:46 UTC ( #525160=note: print w/ replies, xml ) Need Help??


in reply to HASH Array advice

There's no specific reason not to.

If you are only making a small hash (with low numbers) you may find that an array is easier and more efficient *but* there are benefits in using a hash this way over using an array:

Particularly if you have to use a very large index number in the array; I believe that an array is created with (empty where not defined) elements up to the size of the highest index number, so by using a large index number you are pre-allocating (and therefore wasting) a chunk of un-needed memory.

On the other hand with an array you can just do:

for my $element (@array)
and get all the elements back in order (which may or may not be useful in the case you are looking at), whereas with a hash you would have to sort the keys:
for my $key (sort keys %hash)
or explicitly ask for them:
for my $key (@array_of keys)
(which obviously means using a hash *and* an array!) because a hash does not store the keys (or values) in numerical order.


Comment on Re: HASH Array advice
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2015-07-03 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (56 votes), past polls