Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I do not get this

Status
Not open for further replies.

womp

Technical User
Apr 6, 2001
105
US
I have an interactive perl script. The script Adds, Finds or Deletes a user from a Unix system.

print "Add, Find, Delete, or Exit (AFDE)? ";
$answer = <STDIN>;
chomp $answer;
while ($answer ne /^E\b/i) {
find_user();
}elsif ($answer =~ /^A\b/i ) {
add_user();
}elsif ($answer =~ /^D\b/i ) {
find_user();
find_deletees();
questions();
}
}

When I run this I keep getting the error
Use of uninitialized value in pattern match (m//) at line 129 <STDIN> line 1

line 129 is "while" and after trying everything I can think of I cannot get rid of this error.
Does anyone else see where I have messed up

Thanks
 
- Check your syntax first:
Code:
while ($answer ne /^E\b/i) {      # <--- 'ne' or =~ ???
   # bla bla
}elsif ($answer =~ /^A\b/i ) {    # <--- elsif w/ while ??
   # bla bla
}
 
not sure y ur' using while compared to if, as the rest of ur statements are elsif
 
More than likely this line is causing the error:

Code:
while ($answer ne /^E\b/i) {

you are incorrectly using "ne" instead of !~:

Code:
while ($answer !~ /^E\b/i) {

Your code translate to something like this:

Code:
while ($answer ne $_ =~ /^E\b/i) {

and since $_ is not initialized you get the warning message you are getting. You don't need to use regexps to check the input value, it can be done more efficiently using string comparison operators:

Code:
print "Add, Find, Delete, or Exit (AFDE)? ";
            $answer = <STDIN>;
            chomp $answer;
      while (lc $answer ne 'e') {
             find_user();
         }elsif (lc $answer eq 'a' ) {
             add_user();
       }elsif (lc $answer eq 'd' ) {
             find_user();
             find_deletees();
             questions();
       }else {
             print "Invalid option: $answer\n";
        }
  }
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
that should have been:

Code:
print "Add, Find, Delete, or Exit (AFDE)? ";
            $answer = <STDIN>;
            chomp $answer;
      while (lc $answer ne 'e') {
             find_user();
         }if (lc $answer eq 'a' ) {
             add_user();
       }elsif (lc $answer eq 'd' ) {
             find_user();
             find_deletees();
             questions();
       }else {
             print "Invalid option: $answer\n";
        }
  }
}

although it will depend on what find_user() does if you really want to use a "while" loop.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top