0

私は2つのテーブルを持っています。特定の条件が満たされた場合に、あるファイルから別のファイルに値をコピーしたい。これは、すべてがどのように見えるかです

表1

C1    C2    C3
1     a     b
3     e     f

表 2

C1    C2    ...  ...    C7    C8 ...
      1          
      2
      3

表 2 は次のようになります。

C1    C2    ...  ...    C7    C8 ...
      1                       x=b  
      2          
      3                       x=f

したがって、C1 (テーブル 1) と C2 (テーブル 2) の値が同じ場合、テーブル 1 の C3 の値をテーブル 2 の列 C8 に入れる必要があります。C8 の新しい値はすべて「x=」で始まり、その後に続く必要があります。表1の対応する値によって

これは、これまでデータを開くために使用しているスクリプトです

my $data1 = $ARGV[0];
my $data2 = $ARGV[1];

unless ($data1) {
    print "Enter filename:\n";
    $data1 = <STDIN>;
    chomp $data1;}
open(DATA1,'<',$data1) or die "Could not open file $filename $!";

unless ($data2) {
    print "Enter filename:\n";
    $data2 = <STDIN>;
    chomp $data2;}
open(DATA2,'<',$data2) or die "Could not open file $filename $!";

while (<DATA1>) {
    my @columns = split /\s+/;
    next  if /^\s*C/;
      # I'm doing some transformations here so that my input DATA1 has the format of  table 1 in my example
    }

foreach $c1(sort {$a<=>$b} keys %info ) {
    print $c1, "\t", 
      join(',',@{$info{$c1}->{variant}}), "\t", 
      join(',',@{$info{$c1}->{frequency}}), "\n";
# so $c1 is actually how table 1 in my example looks like
}

my %hash;

while($c1){
     my @columns = split;
     $hash{$columns[0]} = [$columns[2]];
     }

while (<DATA2>) {
     my @columns = split;               
     $columns[7] = @{ $hash{ $columns[0] } }[2] if exists $hash{ $columns[0] };
     print "\t", join (@columns), "\n";
     }

これは、@choroba が提供するソリューションを使用したスクリプトです。ただし、画面に何も出力されないため、何かが間違っているに違いありません。

また、値をコピーするときにステートメント「x =」を追加するにはどうすればよいですか?

4

2 に答える 2

2

ハッシュを使用して最初のファイルを記憶します。

#!/usr/bin/perl
use warnings;
use strict;
use feature qw(say);

my ($data1, $data2) = @ARGV;

open my $T1, '<', $data1 or die $!;
my %hash;
while (<$T1>) {
    my @fields = split;
    $hash{$fields[0]} = [ @fields[ 1, 2 ] ];
}

open my $T2, '<', $data2 or die $!;
while (<$T2>) {
    my @fields = split;
    @fields[1, 2] = @{ $hash{ $fields[0] } }[0, 1] if exists $hash{ $fields[0] };
    say join "\t", @fields;
}
于 2013-04-17T15:07:57.240 に答える
0

これを行うには、最初のファイルにハッシュを使用できます。

my %columns1;
while (<DATA1>) {
    my @c = split /\s+/;
    $columns1{$c[0]} = [ @c ];
    next  if /^\s*C/;}
my %columns2;
while (<DATA2>) {
     my @c = split /\s+/;
     if (defined $columns1{$c[0]}) {
        $c[1] = $columns1{$c[0]}[1];
        $c[2] = $columns1{$c[0]}[2];
     }
     $columns2{$c[0]} = [ @c ];
}
于 2013-04-17T15:10:02.267 に答える