如何在Perl数组中搜索更改每次迭代的字符串

时间:2019-02-25 00:17:49

标签: arrays string perl search hashtable

我看过文档,而且我知道如何在数组中搜索字符串,打印字符串的位置等。但是我的问题是,每次迭代我的字符串都会改变。我的目标是在数组的第一行中搜索字符串,保存其位置,然后在数组的第一列中搜索其他字符串,然后保存该位置(为我提供行,数组中的列位置)。这是我要执行的“想法”的代码:

use strict;
use warnings;
my $subseq1 = 'MNIDDKL';
my $subseq2 = 'GLFLKCGGIDEMQSS';
my $line;
my @array;
my @arr;
while($line = <MATFILE>) #Load the array
{
    $line =~ /^$/ and die "Blank line detected at $.\n";
    $line =~ /^#/ and next;
    push @array, $line; #adds each line to the array
};
close MATFILE;

#Join then split
my $joined = join('', @array);
my @rep = $joined =~/./g;
@rep = split(' ', $joined);

#Need to split into row, column format:
while (@rep)
{
    push (@arr, [splice(@rep, 0, 24)]);
}
my ($len1, $len2) = map length, $subseq1, $subseq2;
my @subseq1 = $subseq1 =~ /./g;
my @subseq2 = $subseq2 =~ /./g;
for my $x (0..$len2) #Number of rows
{
    for my $y (0..$len1) #Number of columns
    {
        if ($subseq1[$y] == $arr[0][$_]) #Does not work 
        {
        my $subcol = $_; #column from the sub matrix
        }

    if ($subseq2[$x] == $arr[$_][0]) #Does not work
        {
        my $subrow = $_; #row from pam matrix
        }
    }
}

我知道我可以使用List :: MoreUtils'first_index'查找字符串的第一个位置,但是我不确定随着字符串的变化该怎么做。

总体而言,我正在寻找每个字符串中的字符(例如'A'和'R'),在数组的第一行中找到一个字符,在数组的第一列中找到另一个,并进行关联它的位置。任何帮助都将不胜感激!

这是我要访问的矩阵:

#
# PAM 250 substitution matrix, scale = ln(2)/3 = 0.231049
#
# Expected score = -0.844, Entropy = 0.354 bits
#
# Lowest score = -8, Highest score = 17
#
   A  R  N  D  C  Q  E  G  H  I  L  K  M  F  P  S  T  W  Y  V  B  Z  X  *
A  2 -2  0  0 -2  0  0  1 -1 -1 -2 -1 -1 -3  1  1  1 -6 -3  0  0  0  0 -8
R -2  6  0 -1 -4  1 -1 -3  2 -2 -3  3  0 -4  0  0 -1  2 -4 -2 -1  0 -1 -8
N  0  0  2  2 -4  1  1  0  2 -2 -3  1 -2 -3  0  1  0 -4 -2 -2  2  1  0 -8
D  0 -1  2  4 -5  2  3  1  1 -2 -4  0 -3 -6 -1  0  0 -7 -4 -2  3  3 -1 -8
C -2 -4 -4 -5 12 -5 -5 -3 -3 -2 -6 -5 -5 -4 -3  0 -2 -8  0 -2 -4 -5 -3 -8
Q  0  1  1  2 -5  4  2 -1  3 -2 -2  1 -1 -5  0 -1 -1 -5 -4 -2  1  3 -1 -8
E  0 -1  1  3 -5  2  4  0  1 -2 -3  0 -2 -5 -1  0  0 -7 -4 -2  3  3 -1 -8
G  1 -3  0  1 -3 -1  0  5 -2 -3 -4 -2 -3 -5  0  1  0 -7 -5 -1  0  0 -1 -8
H -1  2  2  1 -3  3  1 -2  6 -2 -2  0 -2 -2  0 -1 -1 -3  0 -2  1  2 -1 -8
I -1 -2 -2 -2 -2 -2 -2 -3 -2  5  2 -2  2  1 -2 -1  0 -5 -1  4 -2 -2 -1 -8
L -2 -3 -3 -4 -6 -2 -3 -4 -2  2  6 -3  4  2 -3 -3 -2 -2 -1  2 -3 -3 -1 -8
K -1  3  1  0 -5  1  0 -2  0 -2 -3  5  0 -5 -1  0  0 -3 -4 -2  1  0 -1 -8
M -1  0 -2 -3 -5 -1 -2 -3 -2  2  4  0  6  0 -2 -2 -1 -4 -2  2 -2 -2 -1 -8
F -3 -4 -3 -6 -4 -5 -5 -5 -2  1  2 -5  0  9 -5 -3 -3  0  7 -1 -4 -5 -2 -8
P  1  0  0 -1 -3  0 -1  0  0 -2 -3 -1 -2 -5  6  1  0 -6 -5 -1 -1  0 -1 -8
S  1  0  1  0  0 -1  0  1 -1 -1 -3  0 -2 -3  1  2  1 -2 -3 -1  0  0  0 -8
T  1 -1  0  0 -2 -1  0  0 -1  0 -2  0 -1 -3  0  1  3 -5 -3  0  0 -1  0 -8
W -6  2 -4 -7 -8 -5 -7 -7 -3 -5 -2 -3 -4  0 -6 -2 -5 17  0 -6 -5 -6 -4 -8
Y -3 -4 -2 -4  0 -4 -4 -5  0 -1 -1 -4 -2  7 -5 -3 -3  0 10 -2 -3 -4 -2 -8
V  0 -2 -2 -2 -2 -2 -2 -1 -2  4  2 -2  2 -1 -1 -1  0 -6 -2  4 -2 -2 -1 -8
B  0 -1  2  3 -4  1  3  0  1 -2 -3  1 -2 -4 -1  0  0 -5 -3 -2  3  2 -1 -8
Z  0  0  1  3 -5  3  3  0  2 -2 -3  0 -2 -5  0  0 -1 -6 -4 -2  2  3 -1 -8
X  0 -1  0 -1 -3 -1 -1 -1 -1 -1 -1 -1 -1 -2 -1  0  0 -4 -2 -1 -1 -1 -1 -8
* -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8  1

我将访问这些值,并使用它们来计算序列相似性的分数。

1 个答案:

答案 0 :(得分:0)

my %score_lookup;
{
   my $found_header = 0;
   my @col_ids;
   while (<>) {
      next if /^#/;

      if ($found_header) {
         my ($row_id, @fields) = split;
         for my $i (0..$#fields) {
            my $col_id = $col_ids[$i];
            my $score = $fields[$i];
            $score_lookup{ $row_id . $col_id } = $score;
         }
      } else {
         $found_header = 1;
         @col_ids = split;
      }
   }
}

my $score = $score_lookup{ "A" . "R" };   # -2