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";
}
}
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";
}
}