【问题标题】:Perl- How can I access a variable when I set I used <STDIN> for user to set it within a loop?Perl-当我设置我使用 <STDIN> 让用户在循环中设置它时如何访问变量?
【发布时间】:2017-07-13 07:07:43
【问题描述】:

我编写了一个脚本,要求用户输入一个保存为变量的响应。问题是我使用了一个循环来执行此操作,并且我有第二个问题需要用户输入,但这在第一个循环之外。第二个问题的输出要求第一个变量和第二个变量都满足打印响应的条件。我收到未明确声明变量的错误消息(使用警告)。

如何在循环结束后提示 Perl“记住”用户从 in 循环中输入的内容?我应该继续循环吗?如果有,怎么做?

感谢您的帮助。我附上了剧本。我认为它很简单,使用不同类型的循环可能会更好,但我只是掌握了 for fxn 的窍门,所以坚持下去会很有帮助。清理代码的任何其他技巧都会有所帮助。

#!C:\Perl
#lesson2XUE.pl
use warnings; use strict;

my $fas="MTESMIRDVELAEEALPKKTGGPQG";
my $swi="MSTESMIRDV ELAEEALPKK TGGPQG";
print "Your sequence has been determined. How would you like it displayed? FASTA/SwissProt:";

for ( my $i=0; $i<4; $i++ ) {
my $choice=<STDIN>;
chomp $choice;
if ($choice =~ /FASTA/i) { 
    print $fas,"\n";
    last;
} elsif ($choice =~ /SwissProt/i){
    print $swi,"\n";
    last;
} else {
        print qq[Please type "FASTA" or "SwissProt" only\n]; 
}       
}

sleep (1);
print "\n";

print qq[Would you like to see the other version? Y/N\n];
my $choice2=<STDIN>;
chomp $choice2;
    if (($choice2 =~ /Y/i) && **($choice =~ /FASTA/i)**){ 
        print $swi,"\n";
} elsif (($choice2=~/Y/i) && **($choice=~/SwissProt/i)**) {
        print $fas, "\n";
} elsif ($choice2 =~ /N/i) {
        print qq[Okay,have a good day];
}   

问题出现在脚本为粗体的地方。因为我不再在循环中,所以 $choice 没有定义。

if (($choice2 =~ /Y/i) && **($choice =~ /FASTA/i)){** 和 } elsif ($choice =~ /SwissProt/i)(意识到我不能在“脚本”视图中加粗)

再次感谢!

【问题讨论】:

  • 你在循环中声明$choice,这就是它的词法范围。如果你想在循环外访问它,就在循环外声明它。
  • 对,我在其他问题上阅读了这篇文章。但是我如何声明用户输入的内容呢?如果我再次使用 它将等待更多的用户输入。如何重新声明用户输入?
  • 循环外:my $choice; 循环内:$choice = &lt;STDIN&gt;;
  • 谢谢!现在我得到了一个不同的错误,尽管“在第 30 行, 第 2 行的模式匹配 (m//) 中使用未初始化的值 $choice”。非常感谢您的帮助!
  • 在循环外声明$choice 正是您需要做的。由于我无法看到你的屏幕并知道你做了什么,你应该edit你的问题额外包含新信息。

标签: perl loops variables stdin


【解决方案1】:

如前所述...在循环范围之外声明 $choice。在您的示例中 $choice 在离开 for 循环后超出范围...

use warnings; use strict;

my $fas="MTESMIRDVELAEEALPKKTGGPQG";
my $swi="MSTESMIRDV ELAEEALPKK TGGPQG";
print "Your sequence has been determined. How would you like it displayed? FASTA/SwissProt:";
my $choice="";
for (my $i = 0; $i < 4; $i++) {
$choice=<STDIN>;
chomp $choice;
if ($choice =~ /FASTA/i) { 
    print $fas,"\n";
    last;
} elsif ($choice =~ /SwissProt/i){
    print $swi,"\n";
    last;
} else {
        print qq[Please type "FASTA" or "SwissProt" only\n]; 
}       
}

sleep (1);
print "\n";

print qq[Would you like to see the other version? Y/N\n];
my $choice2=<STDIN>;
chomp $choice2;
    if (($choice2 =~ /Y/i) && ($choice =~ /FASTA/i)){ 
        print $swi,"\n";
} elsif (($choice2=~/Y/i) && ($choice=~/SwissProt/i)) {
        print $fas, "\n";
} elsif ($choice2 =~ /N/i) {
        print qq[Okay,have a good day];
} 

【讨论】:

    【解决方案2】:

    我认为这些会有所帮助。

    while(defined($line=<STDIN>)){
        print "I saw $line";
    }
    

    我做了一个版本:

    use warnings;
    use strict;
    
    my $fas = "MTESMIRDVELAEEALPKKTGGPQG";
    my $swi = "MSTESMIRDV ELAEEALPKK TGGPQG";
    
    my $tmp = "";
    
    main();
    
    sub main () {
        print
    "Your sequence has been determined. How would you like it displayed? FASTA/SwissProt:";
        while ( defined( my $line = <STDIN> ) ) {
            chomp $line;
    
            if ( $tmp eq "" ) {
                process($line);
                next;
            }
            else {
                process2($line);
                last;
            }
        }
    }
    
    sub process {
        my $choice = shift;
        $tmp = $choice;
        if ( $choice =~ /FASTA/i ) {
            print $fas, "\n";
        }
        elsif ( $choice =~ /SwissProt/i ) {
            print $swi, "\n";
        }
        else {
            $tmp = "";
            print qq[Please type "FASTA" or "SwissProt" only\n];
            return;
        }
        print "\n";
        print qq[Would you like to see the other version? Y/N\n];
    }
    
    sub process2 {
        my $choice2 = shift;
        if ( ( $choice2 =~ /Y/i ) && ( $tmp =~ /FASTA/i ) ) {
            print $swi, "\n";
        }
        elsif ( ( $choice2 =~ /Y/i ) && ( $tmp =~ /SwissProt/i ) ) {
            print $fas, "\n";
        }
        elsif ( $choice2 =~ /N/i ) {
            print qq[Okay,have a good day];
        }
        else {
            print qq[Good day];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-26
      • 2013-08-10
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多