This is probably pretty easy, but I'm still trying to teach myself perl....so bear with me. Here's the code I've written. Its just a little exercise to help me better understand hashes, subroutines...still covering the basics.
In the first 'while' statement at the bottom,
"%hash = &input;", how can I modify that %hash so that every time the &input is called it changes the hash instead of essentially overwriting it.
I've tried $hash{$key,$value} = &input, but only get strange characters in the result.
As of right now, the while loops are working, and the I'm prompted to enter as many names as I want, but the result is only the last key/value I entered. How would I write it so that the %hash would be modified every time the loop runs instead of a new %hash being created every time.
Any ideas?
Thanks!
________________________________________
Check out
In the first 'while' statement at the bottom,
"%hash = &input;", how can I modify that %hash so that every time the &input is called it changes the hash instead of essentially overwriting it.
I've tried $hash{$key,$value} = &input, but only get strange characters in the result.
As of right now, the while loops are working, and the I'm prompted to enter as many names as I want, but the result is only the last key/value I entered. How would I write it so that the %hash would be modified every time the loop runs instead of a new %hash being created every time.
Code:
use strict;
my %hash;
my $key;
my $value;
sub input {
my $employee;
my $title;
my %hash;
print "Please type employee name.\n";
chomp($employee = <STDIN>);
print "Please type employee title.\n";
chomp($title = <STDIN>);
%hash = ($employee => $title);
}
sub choice {
print "Would you like to add a person? yes or no.\n";
chomp(my $response = <STDIN>);
if ($response eq "yes") {
return 1;
} else {
return 0;
}
}
while (&choice == 1) {
%hash = &input; # THIS IS THE PROBLEM LINE
}
while (($key, $value) = each %hash) {
print "$key is the $value.\n";
}
Any ideas?
Thanks!
![[smile] [smile] [smile]](/data/assets/smilies/smile.gif)
Check out