SPUG:Re: Minimal Typing for Selections; ideas?

Scott Blachowicz scott+spug at mail.dsab.rresearch.com
Mon Jun 9 15:53:17 CDT 2003


SPUG-list-owner <tim at consultix-inc.com> wrote:

> While you were typing this message, I had some time for further testing,
> and I've arrived at the new version included below.
> 
> #! /usr/bin/perl -w
> BEGIN {
> 	@choices = (
> 		'ball', 'ball', 'bald', 'balloon', 'ballroom',
> 	);
> }
> 
> OUTER: while (! defined $choice) {
> 	print "Enter your choice: ";
	$input=<STDIN>;	# $choice undefined on input of ^D
> 	defined $input or last;
> 	chomp $input;
> 
> 	if ( ( @matches = "@choices" =~ /\b(${input}\w*)\b/g ) == 1 ) {

Do you want to enclose that "${input}" in \Q...\E (if I remember correctly)
to quote the input for use in the regexp? Unless you want regexp matching
chars to be typed in by the user.

Also...are any of your choices likely to contain embedded white space?
Maybe you could pick a character that'll never appear in your strings and
use something like this (untested) type of logic:

	my $delim = "\001";
	my @choices = ( ... );
	my $choices = $delim . join($delim, @choices) . $delim;

	...etc...

	if ((@matches = "${choices}" =~ /${delim}\Q${input}\E${delim}/g) == 1) {
	...etc...

Scott

> 		$choice=$1;
> 	}
> 	elsif (@matches > 1) {
> 		print"Matched: @matches\n";
> 		print "Too many matches; type more characters\n";
> 	}
> 	else {
> 		print "No match\n";
> 	}
> }
> defined $choice and print "Your choice is $choice\n";
> 	
> -- 
> 
> -Tim



More information about the spug-list mailing list