我真的很困惑如何将变量传递给不同的语句。我的代码是:
use warnings;
use strict;
use feature qw(say);
say "Please enter the first sequence:";
my $sequence1 = <STDIN>;
$sequence1 = chomp $sequence1;
say "Please enter the second sequence:";
my $sequence2 = <STDIN>;
$sequence2 = chomp $sequence2;
if (length $sequence1 < length $sequence2){
my $sequence2_new = substr $sequence2, length $sequence1;
}
my @sequence1 = split(',', $sequence1);
my @sequence2 = split(',', $sequence2_new);
my $element = scalar @sequence1;
my $num = 0;
for ($a = 0; $a < $element; $a++){
if ($sequence1[$a] = $sequence2[$a]){
$num++;
}
}
my $score = $num % length $sequence2;
say "The alignment score is: $score";
在这种情况下,此程序将返回Global symbol "$sequence2_new" requires explicit package name at alignment_sequence.pl line 19.
错误。如果我移动&#34;我的&#34;第14行if语句前面的声明,如my $sequence2_new;
,它会给我Use of uninitialized value $sequence2_new in split at alignment_sequence.pl line 20, <STDIN> line 2.
警告。
答案 0 :(得分:3)
您需要在my $sequence2_new
语句之外声明if
。如你所知,变量的生命在if
块
另请注意
$sequence1 = chomp $sequence1
错了。它会将$sequence1
设置为chomp
删除的字符数 - 可能为1或0.您只想
chomp $sequence1
您还有if ( $sequence1[$a] = $sequence2[$a] ) { ... }
分配。大概你想要比较器eq
吗?
以下是我认为您的代码应该如何显示的内容,但如果$sequence2
长于$sequence1
,我完全不能确定它是否已切断use strict;
use warnings;
use feature qw(say);
print "Please enter the first sequence: ";
chomp ( my $sequence1 = <> );
print "Please enter the second sequence: ";
chomp ( my $sequence2 = <> );
my $sequence2_new;
if ( length $sequence1 < length $sequence2 ) {
$sequence2_new = substr $sequence2, length $sequence1;
}
my @sequence1 = split /,/, $sequence1;
my @sequence2 = split /,/, $sequence2_new;
my $num = 0;
for my $a ( 0 .. $#sequence1 ) {
++$num if $sequence1[$a] eq $sequence2[$a];
}
的开头。这看起来并不合适,但我无法确定
Dim LastRow As Long
LastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row