Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to Use Hash? Array? Still confused after all these years.

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 ;).

%State_files = ( 'midwest.txt' => [ qw(Illinois ...) ], 'east.txt' => [ ('New York', ... ) ], ... );

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. :)

%File_states = ( Illinois => 'midwest.txt', 'New York' => 'east.txt', ... );
--
brian d foy <brian@stonehenge.com>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found