# 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(); # }
In reply to Re^2: Use Hash? Array? Still confused after all these years.
by Anonymous Monk
in thread Use Hash? Array? Still confused after all these years.
by hmbscully
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |