1

次のようなテキストファイルでテーブルを取得した場合

  • AB1
  • AC2
  • AD 1
  • ば 3
  • CD2
  • AE1
  • ED2
  • CB2
  • . . .
  • . . .
  • . . .

そして、別のテキスト ファイルに別のシンボル リストを取得しました。このテーブルを次のような Perl データ構造に変換したいと考えています。

  • _ADE。. .
  • A 0 1 1 . . .
  • D 1 0 2 . . .
  • E 1 2 0 . . .
  • . . . . . . .

しかし、選択した記号のみが必要です。たとえば、A、D、E は記号テキストで選択されていますが、B と C は選択されていません。

4

4 に答える 4

2

最初のものには配列を使用し、2 番目のものには 2 次元ハッシュを使用します。最初のものは大まかに次のようになります。

$list[0] # row 1 - the value is "A B 1"

そして、次のようなハッシュ:

$hash{A}{A} # the intersection of A and A - the value is 0

問題を実装する方法を理解することは、私にとって精神的な戦いの約 75% です。ハッシュや配列を出力する方法については詳しく説明しません。これは簡単なことであり、どのように出力したいか、またはどのくらい出力したいかについても完全に明確ではないからです。しかし、配列をハッシュに変換すると、次のようになります。

foreach (@list) {
  my ($letter1, $letter2, $value) = split(/ /);
  $hash{$letter1}{$letter2} = $value;
}

少なくとも、それがあなたが探しているものだと思います。本当に必要な場合は、正規表現を使用できますが、文字列から 3 つの値を抽出するだけではおそらくやり過ぎです。

編集: もちろん、@listファイルからハッシュを直接アセンブルするだけで済みます。しかし、それを理解するのはあなたの仕事であり、私の仕事ではありません。

于 2009-02-12T05:57:48.823 に答える
1

awkでこれを試すことができます:

awk -f matrix.awk yourfile.txt > newfile.matrix.txt

ここで、matrix.awk は次のとおりです。

BEGIN {
   OFS="\t"
}
{
  row[$1,$2]=$3
  if (!($2 in f2)) { header=(header)?header OFS $2:$2;f2[$2]}
  if (col1[c]!=$1)
     col1[++c]=$1
}
END {
  printf("%*s%s\n", length(col1[1])+2, " ",header)
  ncol=split(header,colA,OFS)
  for(i=1;i<=c;i++) {
    printf("%s", col1[i])
    for(j=1;j<=ncol;j++)
      printf("%s%s%c", OFS, row[col1[i],colA[j]], (j==ncol)?ORS:"")
  }
}
于 2014-11-05T12:21:09.973 に答える
0

これを行う別の方法は、2 次元配列を作成することです。

my @fArray = ();
## Set the 0,0th element to "_"
push @{$fArray[0]}, '_';

## Assuming that the first line is the range of characters to skip, e.g. BC
chomp(my $skipExpr = <>);

while(<>) {
    my ($xVar, $yVar, $val) = split;

    ## Skip this line if expression matches
    next if (/$skipExpr/);

    ## Check if these elements have already been added in your array
    checkExists($xVar);
    checkExists($yVar);

    ## Find their position 
    for my $i (1..$#fArray) {
        $xPos = $i if ($fArray[0][$i] eq $xVar);
        $yPos = $i if ($fArray[0][$i] eq $yVar);
    }

    ## Set the value 
    $fArray[$xPos][$yPos] = $fArray[$yPos][$xPos] = $val;
}

## Print array
for my $i (0..$#fArray) {
    for my $j (0..$#{$fArray[$i]}) {
        print "$fArray[$i][$j]", " ";
    }
    print "\n";
}

sub checkExists {
    ## Checks if the corresponding array element exists,
    ## else creates and initialises it.
    my $nElem = shift;
    my $found;

    $found = ($_ eq $nElem ? 1 : 0) for ( @{fArray[0]} );

    if( $found == 0 ) {
        ## Create its corresponding column
        push @{fArray[0]}, $nElem;

        ## and row entry.
        push @fArray, [$nElem];

        ## Get its array index
        my $newIndex = $#fArray;

        ## Initialise its corresponding column and rows with '_'
        ## this is done to enable easy output when printing the array
        for my $i (1..$#fArray) {
            $fArray[$newIndex][$i] = $fArray[$i][$newIndex] = '_';
        }

        ## Set the intersection cell value to 0
        $fArray[$newIndex][$newIndex] = 0;
    }
}

私は参照を処理した方法についてあまり誇りに思っていませんが、ここでは初心者に耐えます (コメントに提案/変更を残してください)。上記の Chris によるハッシュ メソッドは、はるかに簡単に思えます (入力が大幅に減ることは言うまでもありません)。

于 2009-02-12T08:08:57.027 に答える
0

CPANには、潜在的に役立つ多くのsuffがあります。私は多くの目的でData::Tableを使用しています。Data::Pivotも有望に見えますが、私は使用したことがありません。

于 2009-02-12T08:23:59.797 に答える