perl将2个数据文件比较为数组2D,以便找到一对一的匹配

时间:2013-10-21 22:12:14

标签: perl arrays

我正在做一个程序,它使用变量组合(combiData.txt 63行x不同数量的列)来分析数据表(j1j2_1.csv,1000filas x 19列),以选择每个组合的次数在数据表中重复以及哪些行来自(例如,tableData [row] [4])。

我尝试编译它,但是我得到以下消息:

  Use of uninitialized value $val in numeric eq (==) at rowInData.pl line 34.
  Use of reference "ARRAY(0x1a2eae4)" as array index at rowInData.pl line 56.
  Use of reference "ARRAY(0x1a1334c)" as array index at rowInData.pl line 56.
  Use of uninitialized value in subtraction (-) at rowInData.pl line 56.
  Modification of non-creatable array value attempted, subscript -1 at rowInData.pl line 56.
nothing


这是我的代码:

      #!/usr/bin/perl
      use strict;
      use warnings;

      my $line_match;
      my $countTrue;

       open (FILE1, "<combiData.txt") or die "can't open file text1.txt\n";
       my @tableCombi;
       while(<FILE1>) {
       my @row = split(' ', $_);
       push(@tableCombi, \@row);
       }
       close FILE1 || die $!;

       open (FILE2, "<j1j2_1.csv") or die "can't open file text1.txt\n";
       my @tableData;
       while(<FILE2>) {
          my @row2 = split(/\s*,\s*/, $_);
       push(@tableData, \@row2);
       }
       close FILE2 || die $!;

      #function transform combiData.txt variable (position ) to the real value that i have to  find in the data table.
      sub trueVal($){
      my ($val) = $_[0];
      if($val == 7){ return ('nonsynonymous_SNV'); }
      elsif( $val == 14)          { return '1'; }
      elsif( $val == 15)   { return '1';}
      elsif( $val == 16)    { return '1'; }
      elsif( $val == 17)        { return '1'; }
      elsif( $val == 18)      { return '1';}
      elsif( $val == 19)     { return '1';}
      else              { print 'nothing'; }
      }

     #function  IntToStr ( ) , i'm not sure if it is necessary) that transforms $ to strings  ,     to use the function <eq> in the third loop for the array of combinations compared with the data  array .
     sub IntToStr { return "$_[0]"; } 



     for my $combi (@tableCombi) {
     $line_match = 0;

         for my $sheetData (@tableData) {
        $countTrue=0;

       for my $cell ( @$combi) {
             #my $temp =\$tableCombi[$combi][$cell] ;
         #if ( trueVal($tableCombi[$combi][$cell] )  eq $tableData[$sheetData][ $tableCombi[$combi][$cell] - 1 ]   ){
         #if ( IntToStr(trueVal($$temp )) eq IntToStr( $tableData[$sheetData][ $$temp-1] ) ){
         if ( IntToStr(trueVal($tableCombi[$combi][$cell]) )  eq IntToStr($tableData[$sheetData][ $tableCombi[$combi][$cell] -1])  ){
            $countTrue++;}
         if ($countTrue==@$combi){
            $line_match++;
            #if ($line_match < 50){
            print $tableData[$sheetData][4]." ";
            #}
         }  

     }
  }
print $line_match." \n";
 }

2 个答案:

答案 0 :(得分:0)

首先,在应该返回的情况下,我会将return(0);添加到trueVal()。更好看。

然而,您的主要问题似乎是第52行(严重的是,如果您希望人们了解您的错误消息,请添加行号)。 @$combi是数组引用。不是数组。顺便提一下,错误信息清楚地说明了。我假设你想要它是$combi的单词数组?如果是这样,只需添加另一行,如

my @combiArray = split (/ +/, $combi);

$cell重复一遍。 如果这些条目与一个或多个空格不同,则替换/ +/。 确实可能有一种方法可以在同一行中实现这一点,但仅仅因为Perl允许将多个操作放入一行并不意味着你必须这样做。

答案 1 :(得分:0)

使用此代码:

for my $combi (@tableCombi) {
     $line_match = 0;
     for my $sheetData (@tableData) {
         $countTrue=0;
         for my $cell ( @$combi) {

您正在迭代值,而不是数组的索引。

你应该这样做:

for my $combi (@tableCombi) {
     $line_match = 0;
     for my $sheetData (@tableData) {
         $countTrue=0;
         for my $cell ( @$combi) {
             if (IntToStr(trueVal($cell)) eq IntToStr($sheetData->[$cell-1])){
                 $countTrue++;
             }
             if ($countTrue == @$combi){
                 $line_match++;
                 print $tableData[$sheetData][4]." ";
             }  
         }
     }
}