3 Replies - 8503 Views - Last Post: 04 December 2012 - 08:15 AM

#1 Dictator_Rich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-March 12

Unable to print out individual values in a hash

Posted 29 November 2012 - 10:16 AM

I am creating a program in PERL that allows users to enter a name, and a phone number for that name. The name is the key, and the phone number is the value. The name of the hash is numbers. There are 4 sections. One that allows the user to enter the name and number, one that allows the user to enter a name and receive the phone number for that specific name, one that tells the user all of the values in the hash, and one that quits the program. I got every part of the program working except for the part that prints out the individual numbers. For some reason, it does not print anything out. I am new to PERL, and even more new to hashes, so I'm sure it's an easy fix, but no matter what I do, I cannot figure out what is wrong with the program. Here is what the entire program looks like (I am aware that I didn't add anything to check if the user's choice is value yet. I also left in some commented out lines of code of something else that I tried, but did not work):

#!/usr/bin/perl


%numbers;


print "MENU\n
1. Add a name and telephone number.\n
2. Retrieve a telephone number\n
3. List all of the names and telephone numbers in the file\n
4. Quit the menu\n";

$decision = 0;

while ($decision != 4)
{
$decision = <STDIN>;
if ($decision == 1)
{
  print "Enter name:";
  $name = <STDIN>;

  print "Enter phone number:";
  $phone = <STDIN>;
  $numbers{"$name"} = "$phone";

print "MENU\n
1. Add a name and telephone number.\n
2. Retrieve a telephone number\n
3. List all of the names and telephone numbers in the file\n
4. Quit the menu\n";
}

if ($decision == 2)
{
  print "Enter the name that you would like to look up\n";
  $lookup = <STDIN>;
  chomp $lookup;
  @hashKeys = keys( %numbers );
  @hashValues = values( %numbers );
 print "The number for $loopup is $numbers{$lookup}\n";

#print "$hashKeys[$loopup]$hashValues[$lookup] \n";

#$seq = $numbers{$lookup};
#if (defined($seq)) {
#    print "The number is $seq\n";
#}
#else {
#    print "Sorry, I don't know about \"$lookup\"";
#}

print "MENU\n
1. Add a name and telephone number.\n
2. Retrieve a telephone number\n
3. List all of the names and telephone numbers in the file\n
4. Quit the menu\n";


}

if ($decision == 3)
{
print "These are the currently stored names and numbers:\n";
@hashKeys = keys( %numbers );
@hashValues = values( %numbers );

for ($i = 0; $i < @hashKeys; ++$i)
{
  print "$hashKeys[$i]$hashValues[$i] \n";
}

print "MENU\n
1. Add a name and telephone number.\n
2. Retrieve a telephone number\n
3. List all of the names and telephone numbers in the file\n
4. Quit the menu\n";
}
}



Any help would be greatly appreciated. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Unable to print out individual values in a hash

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7731
  • View blog
  • Posts: 13,058
  • Joined: 19-March 11

Re: Unable to print out individual values in a hash

Posted 29 November 2012 - 10:44 AM

basically, I think what you're looking for is something like

foreach $key (keys (%hash)){
  print "$key, $hash{$key}\n";
}


I hope that gets you where you need to be.
Was This Post Helpful? 0
  • +
  • -

#3 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Unable to print out individual values in a hash

Posted 30 November 2012 - 02:05 AM

You should use strict; and use warnings;. This will help you to catch a lot of simple errors in your code (including the typos on lines 41 and 43, where you typed $loopup instead of $lookup).

Beyond that, you didn't specify whether your problem was in the handling of option #2 or #3. If it was #2, fixing the typo on line 41 should take care of that (unless there are other similar typos involved - but use strict; will tell you about them). If it was #3, jon.kiparsky's answer should help you there.

There's also a lot of over-complication in your code - using C-style for instead of for (@list), making parallel arrays of keys and values from a hash and looping over them instead of using the hash directly, repeated code for printing the menu and getting user input instead of putting that in a sub. If you'd like, I'd be happy to show you what that code would look like in more idiomatic Perl, provided that it wouldn't be cheating on your homework if I did so. If it is homework, I'm still happy to show you what I'm talking about, but using different code so that you'll still need to apply the concepts to your program for yourself.
Was This Post Helpful? 0
  • +
  • -

#4 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 106
  • Joined: 12-October 10

Re: Unable to print out individual values in a hash

Posted 04 December 2012 - 08:15 AM

Why are you quoting variables in this line?

$numbers{"$name"} = "$phone";


this should be:
$numbers{$name} = $phone;


That should work for you :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1