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

regex for <STDIN> 3

Status
Not open for further replies.

AIXFinder

IS-IT--Management
Joined
Jan 4, 2007
Messages
97
Location
US
print "Enter sequences for rows: ";
$a = <STDIN>;
chop $a;
print "Enter sequences for cols: ";
$b = <STDIN>;
chop $b;

I want to allow only certain characters (W, X, Y, Z) for the inputs for both of <STDIN>.
If any other characters than W, X, Y, Z are entered, it asks until the correct sets of characters are entered.
Also, the input converts to upper case regardless lower or upper cases entered for the characters (W, X, Y, Z, w, x, y, z).

thx much
 
Code:
my $a = '';
my $b = '';
until ($a !~ /[^WXYZ]/) {
   print "Enter sequences for rows: ";
   chomp ($a = <STDIN>);
   $a = uc($a);
}
until ($b !~ /[^WXYZ]/) {
   print "Enter sequences for cols: ";
   chomp ($b = <STDIN>);
   $b = uc($b);
}

Useful modules for this kind of thing:
IO::Prompt
Term::Prompt

-------------
Cuvou.com | The NEW Kirsle.net
 
Eeek! Don't use "$a" and "$b" for variable names. Those are special variables used by the sort function.


Also, you might as well make a function for inputting if this is going to be a repeated request.

Code:
my $x = promptWXYZ("Enter sequences for rows: ");
my $y = promptWXYZ("Enter sequences for cols: ");

print "x = '$x', y = '$y'\n";

sub promptWXYZ {
	my $prompt = shift;
	my $input;
	do {
		print $prompt;
		chomp ($input = <STDIN>);
	} until ($input =~ /^[WXYZ]+$/i);
	return uc $input;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top