Don't ask to ask, just ask | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Arrays and hashes are both bunches of pigeon holes where you can store values. The difference is how you get at any particular pigeon hole. For an array each pigeon hole has a number (the 'index') which ranges from 0 to one less than the number of pigeon holes. For a hash each pigeon hole has a label and you access the pigeon hole by whatever is written on the label. I understand that PHP only has labeled pigeon holes (hashes), but of course if you label them with a sequence of numbers from 0 to n - 1 the effect is somewhat like using an array (but less efficient in various ways). Perl arrays are fairly efficient for implementing things like stacks and queues using push, pop, shift and unshift. They are also easy to access from either end - $array[-1] is the last element of a (non-empty) array for example. They are good for storing things where the order is important. Arrays are a poor choice if you need to search for stuff in them often. Perl hashes are good for storing things you want to get at by name and where order is unimportant. They are an excellent choice where you need to determine if you already have an entry of a particular name already which often makes them the tool of choice when you need to look stuff up frequently. True laziness is hard work In reply to Re: Difference between array and a hash? (Newbie concerns)
by GrandFather
|
|