Guten tag monks

I have a simple little subroutine which given a question and an "approved" answer list will verify that the user gave a valid answer and if a default answer (from a config file for example) is provided will validate that as well, without questioning. Here is that little bit of code:

. . $runset{CHECK} = &AskQuestion("CHECK - Enter the check mode you want", + my @checkmode = qw/flat cell hier comp/, $conf{CHECK}); . . $runset{CNAMES} = &AskQuestion("CNAMES - Do you want the run Case Sens +itive", my @CS = qw/yes no/, $conf{CNAMES}); . . # Subroutine - AskQuestion # This will take a simple text for an answer and given a predetermined # array of answers will only accept one of the choices # # Obviously this returns a valid answer # sub AskQuestion (*\@;$){ my $prompt = shift; my $conf = pop; my @answers = @_; my $response; while ( ! $response ) { if ( ! $conf ) { print "$prompt [$_[0]]: "; $response = <STDIN>; chomp $response; if ( $response eq "" ) { $response = $answers[0]; } if ( $response eq "?") { $response = ""; print " Valid answers are: \n"; print " @answers\n"; } } else { $response = $conf; } my $match; for ( my $i = 0; $i < @answers ; $i++ ){ if ( $response eq $answers[$i] ){ $match = "true"; return $response; } } if ( ! $match ){ $response = ""; $conf = ""} } }

Now keep in mind I am asking about 50 questions and a fair number of these are "yes no" or "no yes" type questions. The difference being a "yes no" defaults to "yes". Anyway my users want to be able to simply type y or n for these type of questions. Can anyone come up with a way to modify the existing subroutine to allow a y or n answer when the question has a default answer of "yes no" or "no yes".
Thanks muchos


Rhodium

The <it>seeker</it> of perl wisdom.


In reply to The default question subroutine YesNo by Rhodium

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.