Keep It Simple, Stupid | |
PerlMonks |
Re: Hash or Array - Logic Helpby kcott (Archbishop) |
on May 30, 2020 at 05:44 UTC ( [id://11117495]=note: print w/replies, xml ) | Need Help?? |
G'day audioboxer, How you intend to use the data should really drive your choices for data structures. Unfortunately, you've only shown how you currently create the data structure but not its usage. Probably the most important thing to note — and you may already know this — is that arrays are ordered and hashes are unordered. Other general considerations include the size of the data and whether the data structure ever changes after initial creation. You should also be asking specific questions; such as "Do you only ever want to access the last category or do you perhaps need a sorted list of all categories?". Here's a technique for sorting the keys based on "id" values:
Given it looks like IDs are referenced more than NAMEs, you might consider changing the general layout of the structure to:
I'd also suggest taking a look at the core List::Util module. Some of its functions may prove useful; for example, max (for IDs) and first (with descending sort) seem to be likely candidates. — Ken
In Section
Seekers of Perl Wisdom
|
|