kwn has asked for the wisdom of the Perl Monks concerning the following question:

I guess I should've posted more of the code because I
must not have been clear on my question but I'll
try again.
What I need to do is something like:

if (users input)$loc = a specific key in %locs then
return the associated value of that key from %locs.

here is the original post restored as best I can remember:
Please see below for elaboration

based on user input and on the specific key from %locs
that the user input matches; how do I pull specific
values associated with those keys from %locs.
%locs = (one => '\d$\stuff, two => '\d$\things, three => '\d$\files') %servers = {kwn => "\\server1" krs => "\\server2"} my $server = <STDIN>; chomp $server; my $loc = <STDIN>; chomp $loc;

Replies are listed 'Best First'.
Re: pulling value based on input
by moritz (Cardinal) on Sep 25, 2008 at 07:33 UTC
    The item of a %hash can be accessed with $hash{$key}. Read perldata for more information.
Re: pulling value based on input
by mscharrer (Hermit) on Sep 25, 2008 at 09:25 UTC
    Hi,
    please use <code> tags for your code examples and <c></c> for your inline code. Also using perltidy to tidy up your longer code is very much recommended. This makes your posts much more readable.

    To access the read server and location use $servers{$server} and $locs{$loc} as shown below. You can - and should - also check if the given server and location really exists, which can be done with if (exists $servers{$server}) { } etc.

    my %locs = ( one => '\c$\files', two => '\d$\stuff', three => '\d$\things' ); my %servers = ( ksl => '\\server1', krf => '\\server2' ); my $server = <STDIN>; chomp $server; my $loc = <STDIN>; chomp $loc; print $servers{$server}, "\n"; print $locs{$loc}, "\n";
Re: pulling value based on input
by toolic (Bishop) on Sep 25, 2008 at 11:34 UTC
    It is also a good practice to check for the existence of hash keys, particularly when prompting for input via the keyboard:
    if (exists $locs{$loc}) { # do something... } else { die "key $loc does not exist"; }

    Update: I should have read mscharrer's node more closely, as this was already mentioned. But, I provided the link to the docs, so I guess this is not completely redundant.

Re: pulling value based on input
by toolic (Bishop) on Sep 25, 2008 at 22:06 UTC
    I guess I should've posted more of the code because I must not have been clear on my question but I'll try again.
    You have completely replaced your original question and code with this new stuff. Please add your original text back (inside "readmore" tags: see Writeup Formatting Tips) so that the above replies will have the proper context.

    OK. So, what's your new question? Please describe in what way your new code does not behave as you expect. What is your actual output? How does it differ from your expected output? Do you get any warning or error messages when you use warnings; use strict;?

      thanks for the heads up. I fixed it the best i could.
      Nope i dont get any warnings.
Re: pulling value based on input
by kwn (Novice) on Sep 25, 2008 at 21:39 UTC
    I guess I should've posted more of the code because I
    must not have been clear on my question but I'll
    try again. What I need to do is something like:

    if (users input)$loc = a specific key in %locs then return the associated
    value of that key from %locs.
    ## Carries out the users input into an action print "\n"; print "Where do you want to copy from? or type help for help\n"; my $server = <STDIN>; chomp $server; print "What do you want to copy? or type help for help\n"; my $loc = <STDIN>; chomp $loc; if ((exists $servers{$server}) and (exists $locs{$loc})) { print "\n"; print "Ok, Copying $loc Logs from Source: $servers{$server} to tem +porary location:\n"; print "$temp\n\n"; sleep "3"; print "Ok, all done.\n\n"; print "Zipping and moving to FTP Server:\n"; print "$ftp$folfix-$date\n"; } elsif ((my $server = 'help') or (my $loc = 'help')) { print "$sitrep\n"; print "---Log abb:\n"; foreach $locs (sort keys %locs) { print "$locs\n"; } } else { print "Error: unrecognized server\n"; }