2013-08-07 1 views
5

私はユーザーに複数の質問をします。私は2つのタイプの質問があります: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

1つの単語:抽象化。

現在選択しているソリューションは、拡張性が高く、繰り返しコードが多すぎます。

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'; 
} 

を私たちは私たちから複雑さの多くを隠しサブルーチンをprompt書きます:

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 { my($ query、$ default)= @ _; my $ default_yes = lc $ default eq 'y'; my $ yn = $ default_yes? "[Y/n]": "[y/N]"; my $ answer = lcプロンプト( "$ query $ yn:"); $ 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"; 
    } 
} 

関連する問題