in reply to reprompt user input, until validation pass

Surely your teacher wouldn't have given you this assignment without first discussing 'while' loops. See perlsyn.

And why all the 'exit' calls?


Dave

  • Comment on Re: reprompt user input, until validation pass

Replies are listed 'Best First'.
Re^2: reprompt user input, until validation pass
by ansh batra (Friar) on Oct 17, 2011 at 06:00 UTC
    $ip_no = $ARGV[0]; chomp($ip_no); print "$ip_no\n"; $flag=0; while(!$flag) { if($ip_no =~ /\D/ || $ip_no=~ //) { print "enter number\n"; $ip_no=<STDIN>; chomp($ip_no); } else { if ($ip_no%2 ==0) { print "well done bye bye\n"; $flag=1; } else { print "u r expected to enter an odd number\n"; $ip_no=<STDIN>; chomp($ip_no); } } }
      if($ip_no =~ /\D/ || $ip_no=~ //)

      That is the same as saying:

      if($ip_no =~ /\D/ || $ip_no=~ /\D/)

      Because an empty match operator uses the last successfully matched regular expression.

        replace
        -- if($ip_no =~ /\D/ || $ip_no=~ //) ++ if($ip_no =~ /\D/)
      snippet keep on asking enter number, not continuing further, even i tried to pass argument while executing script.