SPUG:Minimal Typing for Selections; ideas?

SPUG-list-owner tim at consultix-inc.com
Mon Jun 9 11:49:40 CDT 2003


On Mon, Jun 09, 2003 at 12:35:21PM -0400, Tom Legrady wrote:
> On Mon, 2003-06-09 at 11:44, Tim Maher wrote:
> > I landed on the
> > 	"@choices" =~ /regex/
> > idea in a moment of inspiration, although I don't think I've ever
> > seen anybody else do that in Perl code.  Which probably means
> > there's a better way. 
> 
> Of course, there's the possibility that this is the better way. Every
> "better way" has a first use.
> 
> But what's the purpose of the loop? You're checking all the options at
> once in "@choices" =~ /regex/, and then checking it (scalar @choices)
> times, thanks to the loop. Or am I missing something?

> 
> Tom

Yes, you're missing the historical baggage that left that extraneous
loop as a remnant. 8-}

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 ) {
		$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
*------------------------------------------------------------*
| Tim Maher (206) 781-UNIX  (866) DOC-PERL  (866) DOC-UNIX   |
| CEO, JAWCAR ("Just Another White Camel Award Recipient")   |
| tim(AT)Consultix-Inc.Com  TeachMeUnix.Com  TeachMePerl.Com |
*+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
|  Watch for my Book: "Minimal Perl for Shell Programmers"   |
*------------------------------------------------------------*



More information about the spug-list mailing list