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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Response from a Chomp 1

Status
Not open for further replies.

AcidHawk

Technical User
Mar 14, 2001
17
0
0
ZA
Hi

I am trying to validate that the user only enters a (Yy/Nn)

this is what i have but it does not work..:( it comes out of a bigger script but the validation is what im having a problem with

Code:
sub ques_noargs
{
		print "Fetch Performance Data for ALL servers in cube Directory [Yy/Nn]?\n\n";
		&check_args;
}

sub check_args
{
	chomp($_=<STDIN>);
	if ($_=~/[yn]/ig)
		{
			&response;
		} else {
			print &quot;\nYou Have Entered and Incorrect Option\n&quot;;
			print &quot;Valid Options are [Yy/Nn]\n\n&quot;;
			&ques_noargs;
		}
}

sub response
{
	if ($_=~/n/ig) 
		{ 
			print &quot;\nUsage: xPerfCollector <ServerName>\n&quot;;
			exit;
		} 
	if ($_=~/y/ig)
		{
			print &quot;\nI must search thru the DIR\n&quot;;
			exit;
		} 
}

the problem comes in when i enter a correct value.. i want the ques_noagrs to loop if the user puts in any thing other than Y,y,n,N - this part works if not beautifully it still works..(TIPS here would be great..)

if i enter a y/Y &quot;I must search thru the DIR&quot; gets printed out and the program exits.. BUT!!! if i enter n/N nothing gets printed out and the program continues from after the response subroutine..

any help would be much appreciated..(Feel free to critisize the style or methods of programming as i would like to improve my overall coding skills)

Rgds
AcidHawk

----
Of All the things I've lost in my life it's my mind I miss the most.
 
Do I get a Vote if I fix this myself.

This is what I did..

Code:
chomp($_ = <STDIN>);
  if (/^[yn]/i)
    {
      if (/^n/i) 
	{
          print &quot;\nUsage: xPerfCollector <ServerName>\n&quot;;
	  &e;
	} 
      if (/^y/i)
	{
	  print &quot;\nI must step thru ALL the servers DIRS\n&quot;;
	  print &quot;new subroutine\n&quot;;
	  &e;
	}
    } else {
	print &quot;\n=====================================\n&quot;;
	print &quot;You Have Entered and Incorrect Option\n&quot;;
	print &quot;Valid Options are [Y/N]\n&quot;;
	print &quot;=====================================\n&quot;;
	&noargs_ques;

Cleaner and a lot less lines..

----
Of All the things I've lost in my life it's my mind I miss the most.
 
You're code verifies that the user entered something that started with yYNn, but it would also accept strings like &quot;Nz&quot; or &quot;yes&quot;. If you want to make sure it was &quot;y&quot;, &quot;Y&quot;, &quot;n&quot; or &quot;N&quot;, do the following:

if ($_=~/^[yn]$/i)

The &quot;$&quot; marks the end of the string.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top