Perl 同时打印和使用两个文件中的数据 pdb1.pdb ATOM 709 CA THR 25-29.789 33.001 72.164 1.00 0.00 原子711 CB THR 25-29.013 31.703 72.370 1.00 0.00 原子734 CG THR 25-29.838 30.458 72.573 1.00 0.00 原子768 CE THR 25-28.541 28.330 71.361 1.00 0.00 pdb2.pdb ATOM 765 N ALA 25-30.838 33.150 73.195 1.00 0.00 原子764 N LEU 26-29.457 33.193 69.767 1.00 0.00 原子783 N VAL 27-30.286 31.938 66.438 1.00 0.00 原子798 N GLY 28-28.076 30.044 64.519 1.00 0.00 期望输出 709 CA 765 N 1.477-29.789 33.001 72.164-30.838 33.150 73.195 709 CA 764 N 2.427-29.789 33.001 72.164-29.457 33.193 69.767 709 CA 783 N 5.844-29.789 33.001 72.164-30.286 31.938 66.438
等等Perl 同时打印和使用两个文件中的数据 pdb1.pdb ATOM 709 CA THR 25-29.789 33.001 72.164 1.00 0.00 原子711 CB THR 25-29.013 31.703 72.370 1.00 0.00 原子734 CG THR 25-29.838 30.458 72.573 1.00 0.00 原子768 CE THR 25-28.541 28.330 71.361 1.00 0.00 pdb2.pdb ATOM 765 N ALA 25-30.838 33.150 73.195 1.00 0.00 原子764 N LEU 26-29.457 33.193 69.767 1.00 0.00 原子783 N VAL 27-30.286 31.938 66.438 1.00 0.00 原子798 N GLY 28-28.076 30.044 64.519 1.00 0.00 期望输出 709 CA 765 N 1.477-29.789 33.001 72.164-30.838 33.150 73.195 709 CA 764 N 2.427-29.789 33.001 72.164-29.457 33.193 69.767 709 CA 783 N 5.844-29.789 33.001 72.164-30.286 31.938 66.438,perl,file,file-handling,Perl,File,File Handling,等等 pdb1.pdb和pdb2.pdb的内容是读取第2、3、6、7和8列中的值,然后使用第6、7、8列进行距离计算 我试过了,但是输出没有打印出来 Perl open(f1,“pdb1.pdb”或die$!); 打开(f2,“pdb2.pdb”或模具$!); 而($line1=)和($line2=)){ @拆分=拆分(“”,$line1); my@fields=split/,$line1; 打印$fields[1],“\n”; 我的$atom1=@{[$line1=~m/\S+/g]}[2];
pdb1.pdb
和pdb2.pdb
的内容是读取第2、3、6、7和8列中的值,然后使用第6、7、8列进行距离计算
我试过了,但是输出没有打印出来
Perl
open(f1,“pdb1.pdb”或die$!);
打开(f2,“pdb2.pdb”或模具$!);
而($line1=)和($line2=)){
@拆分=拆分(“”,$line1);
my@fields=split/,$line1;
打印$fields[1],“\n”;
我的$atom1=@{[$line1=~m/\S+/g]}[2];
我的$no1=@{[$line1=~m/\w+/g]}[3];
我的$x1=@{[$line1=~m/\w+/g]}[6];
我的$y1=@{[$line1=~m/\w+/g]}[7];
我的$z1=@{[$line1=~m/\w+/g]}[8];
我的$atom2=@{[$line2=~m/\w+/g]}[2];
我的$no2=@{[$line2=~m/\w+/g]}[3];
我的$x2=@{[$line2=~m/\w+/g]}[6];
我的$y2=@{[$line2=~m/\w+/g]}[7];
我的$z2=@{[$line2=~m/\w+/g]}[8];
打印$atom1;
对于($f1,$f2){
打印$atom1$no1$x1$y1$z1$atom2$no2$x2$y2$z2“\n”;
}
}
关闭(f1美元);
收盘价(f2美元);
您的代码有很多语法错误。我已经对你的代码做了一些更改,这将使你开始你想要的
首先,使用严格的
和使用警告
通过这种方式,您已经消除了很多噪音
use strict;
use warnings;
open(my $f1, "pdb1.pdb") or die $!;
open(my $f2, "pdb2.pdb") or die $!;
while(defined(my $line1 = <$f1>) and defined(my $line2 = <$f2>))
{
# print "Iam here";
my @splitted = split(' ',$line1);
my @fields = split / /, $line1;
#print $fields[1], "\n";
my $atom1 = @{[$line1 =~ m/\S+/g]}[2];
my $no1 = @{[$line1 =~ m/\w+/g]}[3];
my $x1 = @{[$line1 =~ m/\w+/g]}[6];
my $y1 = @{[$line1 =~ m/\w+/g]}[7];
my $z1 = @{[$line1 =~ m/\w+/g]}[8];
my $atom2 = @{[$line2 =~ m/\w+/g]}[2];
my $no2 = @{[$line2 =~ m/\w+/g]}[3];
my $x2 = @{[$line2 =~ m/\w+/g]}[6];
my $y2 = @{[$line2 =~ m/\w+/g]}[7];
my $z2 = @{[$line2 =~ m/\w+/g]}[8];
#print $atom1;
for ($f1, $f2) {
print "$atom1 $no1 $x1 $y1 $z1 $atom2 $no2 $x2 $y2 $z2 \n";
}
}
close ($f1);
close ($f2);
如果按上述方式拆分一行,则会得到数组中的所有列。现在你的第1列在第0个索引中,第2列在第一个索引中,依此类推
所以要得到第一列,你应该这样做
my $col1 = @splitted[0];
如果您使用这些正则表达式只是为了获取列,那么它就不需要了,因为您已经在拆分这些列,并且每个列都独立于数组中
更新:
您遇到的问题是,您使用文件句柄进行迭代,这导致了问题
use strict;
use warnings;
open(my $f1, "<pdb1.pdb") or die "$!" ;
open(my $f2, "<pdb2.pdb") or die "$!" ;
my @in1 = <$f1>;
my @in2 = <$f2>;
foreach my $file1 (@in1) { #use array to iterate
chomp($file1);
#print "File1 $file1\n";
my $atomno1=(split " ", $file1)[1];
my $atomname1=(split " ", $file1)[2];
my $xx=(split " ", $file1)[5];
my $yy=(split " ", $file1)[6];
foreach my $file2(@in2) {
chomp($file2);
#print "File2 $file2\n";
my $atomno2=(split " ", $file2)[1];
my $atomname2=(split " ", $file2)[2];
my $x=(split " ", $file2)[5];
my $y=(split " ", $file2)[6];
my $dis=sqrt((($x-$xx)*($x-$xx))+ (($y-$yy)*($y-$yy)));
print "$atomno1 $atomname1 $atomno2 $atomname2 $dis $xx $yy $x $y\n" ;
}
#$file1++;
}
close ($f1);
使用严格;
使用警告;
打开(我的$f1,“
将这两个文件读入内存可能是最简单的方法,除非它们是巨大的
此解决方案调用子程序read_file
,从每个文件中构建所有五个感兴趣字段的哈希数组。然后计算增量并重新格式化数据以供输出
使用严格;
使用“全部”警告;
my$f1=读取_文件('file1.txt');
my$f2=读取_文件('file2.txt');
对于我的$r1(@$f1){
对于我的$r2(@$f2){
my($dx,$dy,$dz)=映射{$r1->{$}-$r2->{$}qw/xyz/;
my$delta=sqrt($dx*$dx+$dy*$dy+$dz*$dz);
我的@rec=(
@{$r1}{qw/id name/},
@{$r2}{qw/id name/},
sprintf('%5.3f',$delta),
@{$r1}{qw/x y z/},
@{$r2}{qw/x y z/},
);
打印“@rec\n”;
}
}
子读取文件{
我的($file\u name)=@;
打开我的$fh,“(u open(f1),”请解释@ruchikaI我不知道如何将上述内容打印为代码(脚本)但是它只运行一次,因为文件1是只读的。我需要的是在文件1中的每一行对文件2中的每一行运行程序,直到文件1的结尾。现在只有文件1的第一行和文件2的每一行都在运行。你知道如何让代码在文件1中的每一行都运行吗file1@ruchika那有用吗?
use strict;
use warnings;
open(my $f1, "<pdb1.pdb") or die "$!" ;
open(my $f2, "<pdb2.pdb") or die "$!" ;
my @in1 = <$f1>;
my @in2 = <$f2>;
foreach my $file1 (@in1) { #use array to iterate
chomp($file1);
#print "File1 $file1\n";
my $atomno1=(split " ", $file1)[1];
my $atomname1=(split " ", $file1)[2];
my $xx=(split " ", $file1)[5];
my $yy=(split " ", $file1)[6];
foreach my $file2(@in2) {
chomp($file2);
#print "File2 $file2\n";
my $atomno2=(split " ", $file2)[1];
my $atomname2=(split " ", $file2)[2];
my $x=(split " ", $file2)[5];
my $y=(split " ", $file2)[6];
my $dis=sqrt((($x-$xx)*($x-$xx))+ (($y-$yy)*($y-$yy)));
print "$atomno1 $atomname1 $atomno2 $atomname2 $dis $xx $yy $x $y\n" ;
}
#$file1++;
}
close ($f1);