作爲一名生物學學生,我試圖擴展自己的編程知識,並遇到了一個Perl問題。Perl中的範圍
我正試圖創建一個程序,生成隨機DNA字符串並執行生成的數據的分析工作。
在程序的第一部分中,我能夠打印出存儲在數組中的字符串,但是第二部分我無法檢索除數組中的一個元素之外的所有元素。
這可能是Perl範圍規則的一部分嗎?
#!usr/bin/perl
# generate a random DNA strings and print it to file specified by the user.
$largearray[0] = 0;
print "How many nucleotides for the string?\n";
$n = <>;
$mylong = $n;
print "how many strings?\n";
$numstrings = <>;
# @largearray =();
$j = 0;
while ($j < $numstrings) {
$numstring = ''; # start with the empty string;
$dnastring = '';
$i = 0;
while ($i < $n) {
$numstring = int(rand(4)) . $numstring; # generate a new random integer
# between 0 and 3, and concatenate
# it with the existing $numstring,
# assigning the result to $numstring.
$i++; # increase the value of $i by one.
}
$dnastring = $numstring;
$dnastring =~ tr/0123/actg/; # translate the numbers to DNA characters.
#print $dnastring;
#print "\n";
$largearray[j] = $dnastring; #append generated string to end of array
#print $largearray[j];
#print $j;
#IN HERE THERE ARE GOOD ARRAY VALUES
#print "\n";
$j++;
}
# ii will be used to continuously take the next couple of strings from largearray
# for LCS matching.
$mytotal = 0;
$ii = 0;
while ($ii < $numstrings) {
$line = $largearray[ii];
print $largearray[ii]; #CANNOT RETRIEVE ARRAY VALUES
print "\n";
$ii++;
@string1 = split(//, $line);
$line = $largearray[ii];
#print $largearray[ii];
#print "\n";
$ii++;
chomp $line;
@string2 = split(//, $line);
$n = @string1; #assigning a list to a scalar just assigns the
#number of elements in the list to the scalar.
$m = @string2;
$v = 1;
$Cm = 0;
$Im = 0;
$V[0][0] = 0; # Assign the 0,0 entry of the V matrix
for ($i = 1; $i <= $n; $i++) { # Assign the column 0 values and print
# String 1 See section 5.2 of Johnson
# for loops
$V[$i][0] = -$Im * $i;
}
for ($j = 1; $j <= $m; $j++) { # Assign the row 0 values and print String 2
$V[0][$j] = -$Im * $j;
}
for ($i = 1; $i <= $n; $i++) { # follow the recurrences to fill in the V matrix.
for ($j = 1; $j <= $m; $j++) {
# print OUT "$string1[$i-1], $string2[$j-1]\n"; # This is here for debugging purposes.
if ($string1[ $i - 1 ] eq $string2[ $j - 1 ]) {
$t = 1 * $v;
}
else {
$t = -1 * $Cm;
}
$max = $V[ $i - 1 ][ $j - 1 ] + $t;
# print OUT "For $i, $j, t is $t \n"; # Another debugging line.
if ($max < $V[$i][ $j - 1 ] - 1 * $Im) {
$max = $V[$i][ $j - 1 ] - 1 * $Im;
}
if ($V[ $i - 1 ][$j] - 1 * $Im > $max) {
$max = $V[ $i - 1 ][$j] - 1 * $Im;
}
$V[$i][$j] = $max;
}
} #outer for loop
print $V[$n][$m];
$mytotal += $V[$n][$m]; # append current result to the grand total
print "\n";
} # end while loop
print "the average LCS value for length ", $mylong, " strings is: ";
print $mytotal/ $numstrings;
包裝邏輯功能。明碼是你知道的一塊***。評論可以幫助,但請閱讀功能。而且,命名規則也很糟糕。你應該使用s_n_a_k_e或cAmEl的情況。 – gaussblurinc
你沒有範圍你的變量。 –
任何未聲明的變量都是全球性的,包括任何拼寫錯誤。這是一個不幸的Perl默認。打開'use strict'和'use warnings',然後通過確定所有這些變量範圍的過程。這很難,但如果你現在不這樣做,它只會變得更糟。 – Schwern