in reply to Checking input against known
#!/usr/bin/perl -w use strict; my @known = qw( foo bar baz ); print "Talk to me, Goose."; chomp(my $input = <STDIN>); print "You gave me $input"; if (grep { $_ eq lc($input) } @known) { print ", I know about that. Have a nice day.\n"; } else { print ", I don't know that, try again.\n"; }
That's possibly not super-efficient, as grep has to examine the whole list each time. If that was a concern, then you could make use of the first() function from List::Util.
Small nit: It's generally considered bad™ coding style to include multiple statements on a single line. Whitespace and newlines are quite affordable these days - use them ;)
Cheers,
Darren :)
|
|---|