Perl: the Markov chain saw | |
PerlMonks |
Re: Use Hash? Array? Still confused after all these years.by brian_d_foy (Abbot) |
on Jul 21, 2005 at 22:21 UTC ( [id://477035]=note: print w/replies, xml ) | Need Help?? |
Use a hash when you want to look something up by a label you give it. The name "hash" is unfortunate because it's really just an implementation detail that it's a hash. The previous Perl name was "associative array" because you associated the value and the label you attached to it. Other languages call the same thing "dictionaries". For your specific problem, you may want to represent the state information in a way that makes sense to you and that you can easily read and modify. Since the files comprise the state data, the easiest way to do that may be to use the file name as the key (that's the label) and the list of states as the value (in an anonymous array, which you already hinted at in your question, so you're already thinking about this way, which means you get more than you give yourself credit for ;).
Once you figure that out, you need to make it so you can start from a state and get back to the file. You don't have to do this all with one hash, so make a reverse mapping. Write a little bit of code that turns %State_files into something that use the state name as the key so you can use it to look up the file name. You'll notice that the Llama has a couple of examples of reverse mapping. :)
-- brian d foy <brian@stonehenge.com>
In Section
Seekers of Perl Wisdom
|
|