Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Difference between array and a hash? (Newbie concerns)

by ig (Vicar)
on Jul 30, 2009 at 05:54 UTC ( [id://784495]=note: print w/replies, xml ) Need Help??


in reply to Difference between array and a hash? (Newbie concerns)

why would it be a bad idea for them to be merged into the same data structure?

Sometimes it is a good idea to merge them. For these occasions there are modules that provide the mapping ability of hashes and the ordering ability of arrays in the same data object.

Often, hash lookup is needed but ordering is not required. In these cases, it would be a waste of resource to maintain un-needed ordering of the hash keys. For small data sets the difference wouldn't be very much but Perl can handle very large hashes. For these large hashes, the work of inserting a new element would be significantly greater if the new key had to be inserted into an ordered list.

Similarly, if all that is needed is an ordered list of items, the implementation of the numerically indexed array is more efficient than that of the hash lookup. One could use a hash with keys 0, 1, 2, etc. but it would be slower than an array for many operations.

So, the reason it would be bad to merge arrays and hashes is that doing so would make many common cases less efficient (slower and/or consuming more resource).

  • Comment on Re: Difference between array and a hash? (Newbie concerns)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found