Perl Monk, Perl Meditation | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
On a user level, not behind the scenes, a hash and an array are exactly the same thing except one takes arbitrary strings/scalars as their keys and arrays uses only non negative numbers. There are nuances to how they are used in perl, but in a general sense this is true.
If you can easily map everything to the non-negative If you need arbitrary access to information where order or mapping isn't easy or makes sense, a hash is great. For instance, if I put my name, address and other personal info in a single structure, a hash rocks. $person{first_name} would give me a person's first_name. To give the counter examples of when they suck, keeping a sorted list of people in a hash would be a little odd. I can do $person{5}, but what would $person{beer} mean in the context? Should it be allowed? It's just, strange. Using an array for data that has no easy mapping to say, a function that is natural, an array kinda sucks. For instance, $person[0] being the first name, $person[1] being the last isn't as natural as $person{first_name}. The rest is just engineering. They are both fairly fast, it's a matter of seeing which tool fits the job better. Update: Wtf.. I used number when I meant integer. I'm an idiot. What i meant and typed were inconsistent. Thanks brian_d_foy.
---- In reply to Re: Use Hash? Array? Still confused after all these years.
by exussum0
|
|