2013-08-07 38 views
5

我想問用戶多個問題。我有兩種類型的問題: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)?"; 
      } 
    } 
+2

爲什麼你在所有地方都用&&?它是我見過的最奇怪的東西。正常語句以分號結尾,而不是AND運算符。 – TLP

+1

另外..你的問題是什麼?如何使用if-else? – TLP

+0

@TLP我認爲&&是必要的,如果你想要在彼此之後做多件事情 – user1987607

回答

18

一個字:抽象。

您當前選擇的解決方案不能很好地擴展,並且包含過多的重複代碼。我們將編寫一個子程序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追加元素。

+0

謝謝Amon,這使得代碼確實很乾淨。由於我是perl新手,我不能自己想出這個。但是我在perldoc中查看了所有內容,並且我現在瞭解了所有的命令。 – user1987607

+0

尼斯阿蒙。我剛剛重用了你的代碼。我稍微改變了'prompt_yn()'以允許採用默認值 'sub prompt_yn {$ query,$ default)= @_; my $ default_yes = lc $ default eq'y'; my $ yn = $ default_yes? 「[Y/N]」:「[Y/N]」; my $ answer = lc提示符(「$ query $ yn:」); return $ default_yes? ! ($ answer =〜/^n /):$ answer =〜/^y /; }' – Xtof

+0

'prompt'沒有內置? –

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(); 
    } 

所以,你可以在許多文件中導入這種方式。

0

前一段時間我結束了以下內容:

#!/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"; 
    } 
} 

相比以前的解決方案這個處理空輸入。