in reply to Trying to understand hashes (in general)
Arrays are good for doing array stuff and hashes are good for doing hash stuff. If you set aside how they work under the hood arrays and hashes are nearly identical (in PHP essentially they are identical). The "difference" between arrays and hashes is that arrays are indexed by numbers and hashes are indexed by strings.
Arrays are really good when you have a list of things you want to store and either they naturally are keyed by a number, or have no key but may be ordered. It's really fast to access element in an array by their index (position in the array). Perl arrays are also very efficient at adding and removing elements at the start and end of the array. Arrays tend to be a poor choice if there are large gaps where index values have been skipped.
Hashes are really good where you want to access values by name. Note that there is no reason the name can't be a number so in that sense hashes and arrays can do the same job. Although hashes are pretty quick at looking up values by name, they aren't as fast as arrays. The other major difference is that hashes don't remember the order that elements were inserted so they generally can't be used in a trivial fashion to store ordered data.
Unless you are generating a set of unique values by taking advantage of the fact that hash keys are unique, it seldom makes sense to use a hash to just store keys so on the face of it a hash doesn't make sense for storing a file system's structure because the file system doesn't allow duplicated names in any case. Nested arrays are a much better fit for a file system's structure.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Trying to understand hashes (in general)
by james28909 (Deacon) on Dec 23, 2014 at 06:03 UTC | |
by GrandFather (Saint) on Dec 23, 2014 at 06:42 UTC | |
by james28909 (Deacon) on Dec 23, 2014 at 07:51 UTC | |
by hexcoder (Curate) on Dec 23, 2014 at 16:26 UTC |