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!

In reply to Using a variable in place of a hash name when calling the value of a specific key by kelder

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.