...

View Full Version : Printing message when user input doesn't match anything in hash



CatchThi5Drift
12-14-2009, 01:52 AM
Hi. I'm trying to get a message to print only if the information a user has input is not anywhere in a dbm file (ie. the dbm file is full of employee details, including their ID number, so I need something like "Sorry, that ID number does not exist" to be printed if the ID number is nonexistent).

Right now, it's printing the message regardless of whether or not the ID number exists.

Any help would be great, thanks :).



sub modify {

my $database = "employee.dbm";
print "\nEnter employee ID number: ";

my $index = <STDIN>;
my %employees;

print "\n";

# Open the DBM database file
dbmopen (%employees, $database, 0644) || die "Could not open DBM file $database : $!\n";

if (my $record = $employees{$index}) {

# Split up the record
($name,$address,$city,$prov,$phone,$email,$dep,$job,$sal) = split (/\|/, $record);

# Print it
print "$id\t $name\t $address\t $city\t $prov\t $phone\t $email\t $dep\t $job\t $sal\n";

print "\nNew Details\n\n";
print "ID number: ";
$id = <STDIN>;
chomp $id;
print "Name: ";
$name = <STDIN>;
chomp $name;
print "Street address: ";
$address = <STDIN>;
chomp $address;
print "City: ";
$city = <STDIN>;
chomp $city;
print "Province (ex. ON): ";
$prov = <STDIN>;
chomp $prov;
print "Phone number (ex. 4165550000): ";
$phone = <STDIN>;
chomp $phone;
print "Email: ";
$email = <STDIN>;
chomp $email;
print "Department (ex. Sales): ";
$dep = <STDIN>;
chomp $dep;
print "Job (ex. Cashier): ";
$job = <STDIN>;
chomp $job;
print "Salary (ex. 9999.99): ";
$sal = <STDIN>;
chomp $sal;

# Save the information into the DBM file
$employees{$id} = "${name}|${address}|${city}|${prov}|${phone}|${email}|${dep}|${job}|${sal}";

# Close the DBM file
dbmclose %employees;

} else {
print STDERR "Sorry, that ID number does not exist";
}
}

oesxyl
12-14-2009, 09:02 AM
Hi. I'm trying to get a message to print only if the information a user has input is not anywhere in a dbm file (ie. the dbm file is full of employee details, including their ID number, so I need something like "Sorry, that ID number does not exist" to be printed if the ID number is nonexistent).

Right now, it's printing the message regardless of whether or not the ID number exists.

Any help would be great, thanks :).



sub modify {

my $database = "employee.dbm";
print "\nEnter employee ID number: ";

my $index = <STDIN>;
my %employees;

print "\n";

# Open the DBM database file
dbmopen (%employees, $database, 0644) || die "Could not open DBM file $database : $!\n";

if (my $record = $employees{$index}) {

# Split up the record
($name,$address,$city,$prov,$phone,$email,$dep,$job,$sal) = split (/\|/, $record);

# Print it
print "$id\t $name\t $address\t $city\t $prov\t $phone\t $email\t $dep\t $job\t $sal\n";

print "\nNew Details\n\n";
print "ID number: ";
$id = <STDIN>;
chomp $id;
print "Name: ";
$name = <STDIN>;
chomp $name;
print "Street address: ";
$address = <STDIN>;
chomp $address;
print "City: ";
$city = <STDIN>;
chomp $city;
print "Province (ex. ON): ";
$prov = <STDIN>;
chomp $prov;
print "Phone number (ex. 4165550000): ";
$phone = <STDIN>;
chomp $phone;
print "Email: ";
$email = <STDIN>;
chomp $email;
print "Department (ex. Sales): ";
$dep = <STDIN>;
chomp $dep;
print "Job (ex. Cashier): ";
$job = <STDIN>;
chomp $job;
print "Salary (ex. 9999.99): ";
$sal = <STDIN>;
chomp $sal;

# Save the information into the DBM file
$employees{$id} = "${name}|${address}|${city}|${prov}|${phone}|${email}|${dep}|${job}|${sal}";

# Close the DBM file
dbmclose %employees;

} else {
print STDERR "Sorry, that ID number does not exist";
}
}
try this change:


if (exists($employees{$index})) {
my $record = $employees{$index};
# Split up the record


oops, I didn't see, where is the code to fetch %employees?
you need to check existence of $index to get "Sorry ...."?

best regards

CatchThi5Drift
12-14-2009, 03:07 PM
Hi, thanks for your help. I got it to work, though. Just needed to add chomp $index; after $index = <STDIN>;.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum