Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Use Hash? Array? Still confused after all these years.

by Anonymous Monk
on Jul 23, 2005 at 02:16 UTC ( [id://477411]=note: print w/replies, xml ) Need Help??


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

You've gone 5 years without learning hashes? Pick up a copy of Learning Perl, and work through it all the way. It will serve you well. Conceptually, you'll want to do something like this:
# first, tell perl you want a local version of a hash called filename my %filename; # put in the data for your states %filename ( state1 => "FileA.csv", state2 => "FileB.csv", state3 => "FileC.csv"); # The above statement is the same as writing the three commented lines + below. It's prefered because it's simpler, and requires less typing, + so it has lower odds for a mistake. Perhaps the lines below are clea +rer to a beginner, though... they do the same thing as the line above +. #$filename{"state1"}="FileA.csv"; #$filename{"state2"}="FileB.csv"; #$filename{"state3"}="FileC.csv"; # get your state variable somehow... say from a function called get_st +ate() $state = get_state(); $file = $filename{ $state }; # The above statement a just looks for a matching value for $state in +the hash. If it can't find one, uses the undefined value instead. It +assigns whatever value it came up with to $file. It's like writing th +e following big if statement, but again, with less repetition... # if ( $state eq "state1" ) { # $file = "FileA.csv"; # } elsif ( $state eq "state2" ) { # $file = "FileB.csv"; # } elsif ( $state eq "state3" ) { # $file = "FileC.csv"; # } else { # $file = undef(); # }

Replies are listed 'Best First'.
Re^3: Use Hash? Array? Still confused after all these years.
by hmbscully (Scribe) on Jul 26, 2005 at 20:55 UTC
    I appreciate your response, but in my question I did note that I do have Learning Perl, aka the llama, I've had a copy forever. I know I need to work it through again.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-20 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found