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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.