8

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

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)?";
            }
    }
4

3 回答 3

23

一个词:抽象。

您当前选择的解决方案扩展性不好,并且包含过多的重复代码。我们将编写一个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.

于 2013-08-07T13:11:53.960 回答
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";
  }
}

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

于 2014-02-28T09:38:23.940 回答
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();
    } 

因此,您可以通过这种方式导入许多文件。

于 2013-08-07T13:18:55.163 回答