向用户提示多个问题(是/否和文件名输入)

时间:2013-08-07 12:33:34

标签: perl

我想问用户多个问题。我有两种类型的问题:Y / N或文件名输入。我不确定如何将这一切都放入一个漂亮的if结构中。而且我不确定我是否也应该使用'else'语句。有人可以帮助我们吗?这就是我到目前为止所做的:

print "Do you want to import a list (Y/N)?"; # first question yes/no
my $input = <STDIN>;
chomp $input;
    if ($input =~ m/^[Y]$/i){ #match Y or y
        print "Give the name of the first list file:\n";
        my $list1 = <STDIN>; 
        chomp $list1;
        print "Do you want to import another gene list file (Y/N)?";
            if ($input =~ m/^[Y]$/i){
                 print "Give the name of the second list file:\n" # can I use $input or do I need to define another variable?;
                 $list2 = <STDIN>;
                 chomp $list2;
                 print "Do you want to import another gene list file (Y/N)?";
            }
    }

3 个答案:

答案 0 :(得分:20)

一个词:抽象。

您当前选择的解决方案无法很好地扩展,并且包含过多的重复代码。我们将编写一个子程序prompt,它隐藏了我们的大部分复杂性:

sub prompt {
  my ($query) = @_; # take a prompt string as argument
  local $| = 1; # activate autoflush to immediately show the prompt
  print $query;
  chomp(my $answer = <STDIN>);
  return $answer;
}

现在要求确认promt_yn

sub prompt_yn {
  my ($query) = @_;
  my $answer = prompt("$query (Y/N): ");
  return lc($answer) eq 'y';
}

我们现在可以用实际工作的方式编写代码:

if (prompt_yn("Do you want to import a list")){
    my $list1 = prompt("Give the name of the first list file:\n");
    if (prompt_yn("Do you want to import another gene list file")){
         my $list2 = prompt("Give the name of the second list file:\n");
         # if (prompt_yn("Do you want to import another gene list file")){
         # ...
    }
}

哦,所以看起来你真的想要一个while循环:

if (prompt_yn("Do you want to import a list")){
    my @list = prompt("Give the name of the first list file:\n");
    while (prompt_yn("Do you want to import another gene list file")){
        push @list, prompt("Give the name of the next list file:\n");
    }
    ...; # do something with @list
}

@list是一个数组。我们可以通过push附加元素。

答案 1 :(得分:1)

前段时间我最终得到了以下内容:

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;


if (&prompt_yn("CONTINUE")){
  my @res = split(" ",&prompt("ENTER INPUT")) ;
  print Dumper @res;
}
else{
  print "EXIT\n";
}

sub prompt_yn{
  my ($query) = @_;
  $query = $query . " (Y/N): ";
  print "$query";
  while (<>) {
    $_ =~ s/^\s+|\s+$//g;
    $_ =~ s/\r|\n//g;
    if ($_ =~ /\S/){
      if ($_ =~ /^y$|^yes$/i){
        # if you want information message about entered value uncomment this
        # print "You have entered Y\n";
        return 1;
      }
      elsif ($_ =~ /^n$|^no$/i){
        # if you want information message about entered value uncomment this
        # print "You have entered N\n";
        return 0;
      }
      else{
        # if you want information message about entered value uncomment this
        # print "You have entered wrong value try again: \n";
      }
    }
    print "$query";
  }
}

sub prompt{
  my ($query) = @_;
  $query = $query . ": ";
  print "$query";
  while (<>) {
    $_ =~ s/^\s+|\s+$//g;
    $_ =~ s/\r|\n//g;
    if ($_ =~ /\S/){
      return $_;
    }
    print "$query";
  }
}

与以前的解决方案相比,它处理空输入。

答案 2 :(得分:0)

您可以使用子例程。 这有助于您在视觉上和逻辑上保持一切在线。 例如


    &main();

    sub main {
        print "Do you want to import a list(Y/N)";
        my $input = ;
        chomp $input;
        if($input =~ m/^[Y]$/i) {
            &importfile();
        } elsif ($input =~ m/^[N]$/i) {
            print "you said no";
        } else {
           print "Invalid option";
        }
    }
    sub importfile
    {
        print "file name please ";
        my $file = STDIN;
        # import and process the file here.....
        &main();
    } 

所以你可以用这种方式导入很多文件。