Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Everyone posted their own views of how and when to use hashes. Problem is that it's really hard to explain in plain words.

I believe it's hard because it either clicks in your head or not. Well at least it's case with me - once I was introduced to hashes I simply got them as granted.

As someone pointed out - you use hashes for storing any data that has names. Such as database tables and forms. Sometimes it's also good to use hash as a parameter for some function/method (often referred as PARAMHASH). In cases when you have more than few parameters.

Anyway if I understood you - I would use following:
- hash for storing the form data > you get it from CGI that way
- hash for storing file names. As keys you have state options, and as matching values you have file names.

So it ends up with something like:

my $request = CGI->new(); # Is it a hashref or plain hash ?!? my $values = $request->Vars(); # Or maybe coming from a config file my %files = { state1 => 'filename1.csv', state2 => 'filename2.csv', default => 'default_filename.csv', } my $file_name = $files{default}; if(defined $files{$values{state}}) { $file_name = $files{$values{state}; } # Or something shorter like : # my $file_name = $files{$values{state}} || $files{default}; #open....


In reply to Re: Use Hash? Array? Still confused after all these years. by techcode
in thread Use Hash? Array? Still confused after all these years. by hmbscully

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-29 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found