![]() |
|
Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
comment on |
( #3333=superdoc: print w/replies, xml ) | Need Help?? |
If I understand your current approach correctly, you have a bunch of meta tags and each tag has a set of data associated with it (value, char count, etc). When you said you're "creating a mess of hashes on the fly", it sounds like you're either using symbolic references (don't do that) or doing something like this: I think what you're looking for here is a hash of hashes (which is actually a hash of hash references). You could combine the %title and %keywords hashes from above into a single hash: Now you've got a single hash to store all your data in, and you don't need to worry about creating hundreds of individually declared hashes. Getting data into this HoH (and accessing it) is simple. Instead of doing: do this: New inner (e.g., value) and outter (e.g., title) keys can be added at will. Keep in mind that the value of $tags{title} is actually a reference to another hash. If you wanted to use the hashrefs instead of accessing the data directly (e.g., passing it to a subroutine), you would dereference it like this: Since all of the inner hashes were set up the same, the subroutine doesn't need to know (or care) about which hashref it gets, all it knows is that there is a key named 'value' in the hash. What if you want to add an array to the HOH? Let's add a new key to the inner hash, whose value is an arrayref:
References are very powerful and provide nearly unlimited flexibility in your data structure. They are well worth learning. The obligatory reference list: perlref, perlreftut, and perldsc. HTH. In reply to Re: Introduction to anonymous arrays and hashes
by bobf
|
|