Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Printing message when user input doesn't match anything in hash

    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 .

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

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by CatchThi5Drift View Post
    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 .

    Code:
    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:
    Code:
        if (exists($employees{$index})) {
            my $record = $employees{$index};
            # Split up the record
    Edit: oops, I didn't see, where is the code to fetch %employees?
    you need to check existence of $index to get "Sorry ...."?

    best regards
    Last edited by oesxyl; 12-14-2009 at 09:08 AM.

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, thanks for your help. I got it to work, though. Just needed to add chomp $index; after $index = <STDIN>;.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •