Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Hash or Array - Logic Help

by kcott (Archbishop)
on May 30, 2020 at 05:44 UTC ( [id://11117495]=note: print w/replies, xml ) Need Help??


in reply to Hash or Array - Logic 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:

$ perl -E ' my %x = (A => { id => 2 }, B => { id => 3 }, C => { id => 1 }); my @sorted = sort { $x{$a}{id} <=> $x{$b}{id} } keys %x; say "Last: $sorted[-1]"; ' Last: B

Given it looks like IDs are referenced more than NAMEs, you might consider changing the general layout of the structure to:

ID => { name => '...', parent_id => ..., }

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11117495]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found