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

I'm trying to start a database of sorts, using hashes named as users that contain the user information such as phone, location, etc. What I want to do is be able to call the proper hash based on what the user inputs into the command line, i.e. what would normally be

"The $desired_info for $Userhash{name} is: $Userhash{$desired_info}."

is instead

"The $desired_info for $<$userinputgoeshere, input name matches the name of the hash>{name} is: $<$userinputagain>{$desired_info}."

This seems extraordinarily complicated, but I'm too much of a neophyte to be able to fix it. Heres the actual code:
#!/usr/bin/perl #Example hashes, ideally I'd like to be able to store/recall hashes an +d be able to generate new #ones. my %BobSmith = ( name => "Bob Smith", phone =>"123-456-7890", street =>"13245 Main Drive", city =>"Salt Lake City", state =>"Utah", zip =>"98765", ); my @BobSmithk = keys(%BobSmith); my %JimJones = ( name => "Jim Jones", phone =>"098-765-4321", street =>"1000 J St", city =>"Jonestown", state =>"Utah", zip =>"12345", ); my @JimJonesk = sort(keys(%JimJones)); mainmenu(); sub mainmenu { print "Request: \n 1. Keys \n 2. Specific Records \n 3. Quit \n Selection: "; my $choice = <>; if ($choice == 1){ showkeys(); } elsif ($choice == 2){ records(); } elsif ($choice == 3){ exit; } else { print "Sorry, that selection isn't possible. Please pick again.\n"; mainmenu(); } } sub showkeys { print "Input user's first name: "; my $userfirstname = <>; $userfirstname = trim($userfirstname); print "\nInput user's last name: "; my $userlastname = <>; $userlastname = trim($userlastname); my $userfull = $userfirstname.$userlastname; print "$userfull\n";
#This was just to see if I was trimming the input correctly
print "The keys for $$userfull{name} are: @$userfullk\n"; mainmenu(); }
#The user enters the desired name, which is called as a variable and used to find the hash of the same name and list out the information on file in the form of keys
sub records { print "Input user's first name: "; my $userfirst = <>; $userfirst = trim($userfirst); print "Input user's last name: "; my $userlast = <>; $userlast = trim($userlast); $userfullname = $userfirst.$userlast; $userfullname = trim($studentfullname); print "$userfullname\n";
#This was just to see if I was trimming the input correctly
print "\nInput record: "; my $record = <>; $record = trim($record); print "The $record of $$userfullname{name} is: $$userfullname{$record}\n"; mainmenu(); } sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }

Everything runs fine, but when I ask for the keys or try to look up a specific bit of info, I get something like "The keys for are: ". Trying to use the input as a variable in place of the actual hash name doesn't seem to be working at all.

I'm sure there are better ways to do this, is there a particular command I should be using? Thank you!

Replies are listed 'Best First'.
Re: Using a variable in place of a hash name when calling the value of a specific key
by bv (Friar) on Aug 26, 2009 at 19:57 UTC

    A better solution would be to use a hash-of-hashes, something like this:

    my %users = ( "Bob Smith" => { name => "Bob Smith", phone =>"123-456-7890", street =>"13245 Main Drive", city =>"Salt Lake City", state =>"Utah", zip =>"98765", }, "Jim Jones" => { name => "Jim Jones", phone =>"098-765-4321", street =>"1000 J St", city =>"Jonestown", state =>"Utah", zip =>"12345", }, ); my $user = "Jim Jones"; print "The keys for $user are: ", join(", ",keys %{$users{$user}});
    Read perlref or perllol for more info
    $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;
Re: Using a variable in place of a hash name when calling the value of a specific key
by roboticus (Chancellor) on Aug 26, 2009 at 20:03 UTC
    kelder:

    You're close: What you want is to add another layer to your hash, here are a couple hints:

    my %Rolodex = { BobSmith => { name => "Bob Smith", phone =>"123-456-7890", }, JimJones => { name => "Jim Jones", phone =>"098-765-4321", }, }; for my $personKey ("BobSmith", "JimJones") { print "Name: %s, Phone: %s\n", $Rolodex{$personKey}{name}, $Rolodex{$personKey}{phone}; } my @BobSmithk = keys(%{$Rolodex{BobSmith}});
    ...roboticus