On Fri, February 12, 2016 12:37 pm, Christin Deville wrote: > I have been lurking for a while but I want to chime in and say thanks for > that piece of advice. I've been trying to sort out in my head when to use > a hash or an array and this all helps! > >
in real world code you should be using hashes way more often than arrays. arrays map to stacks, lists, fifo, etc. which is just a few concepts. hashes map to many more concepts: data structures, sets, tables, dispatching, lookup by name, mapping from one domain to another, inclusion tests, etc. it is almost never a choice of which to use. it should be plain to see when a hash or array is needed. study some good cpan code and you will see that hashes are much more common than arrays in perl. some notes to keep around: arrays are indexed by integers hashes are indexed by strings (keys) arrays are ordered (by their integer indexes) hashes are unordered (keys are not presorted) arrays can be added to at their beginning (unshift) or end (push) or inside (splice). those are all functions. you can delete from the beginning with shift or the end with pop. deleting from the middle with splice(). hashes can be added to by assignment, and keys can be deleted by delete(). you loop over arrays with foreach and get their values you loop over hash keys with foreach/keys() (only get keys), while/each() (can get keys and values). arrays and hashes have very similar syntax. the sigils are @ vs % and the indexing delimiters are [] vs {} (same for anon refs to them). uri -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/