Perl while循环和读取行 [英] Perl while loops and reading lines
问题描述
每条记录有4行:
类似于以下内容:
@NCYC36111a03.q1k bases 1 to 1576
GCGTGCCCGAAAAAATGCTTTTGGAGCCGCGCGTGAAAT
+
!)))))****(((***%%((((*(((+,**(((+**+,
有两个文件,其中一个文件对应到另一个
There are two files in which 1 file corresponded to the other
有一个序列A1
数组,因此一次从文件1读取1条记录。从文件2读取记录。如果序列在记录1文件1(第2行)与数组A1中的序列匹配,我将记录从文件2打印到输出文件,依此类推...但是重点是我需要一次读取记录....我会打破内部循环,以便我可以从文件1中读取下一条记录,然后将其与文件2中的下一条记录进行比较
there are an array of seqeunces A1 So read 1 record at a time from file 1. read record from file 2. if the sequence in record 1 file 1 (line 2) matches the seuqnece in the array A1, i print the record from file 2 to an output file so on...but the point is i need to read a record at a time.... how would i break out of the inner loop so that i can read the next record from the file 1 and then compare it to the next record in file 2
推荐答案
从您的句子我需要检查该序列是否与第二个序列中的任何一个匹配。我收集到您想检查的行中是否 any 行Ť文件是否匹配?
From your sentence I need to check if the sequence matches any with the sequence from the second I gather that you want to check whether any lines in the two files match?
如果您需要多次读取文件,则可以使用 seek
快退到
If you need to read a file several times then you can use seek
to rewind to the start of it without reopening it.
此程序显示了这个想法。
This program shows the idea.
use strict;
use warnings;
open my $fh1, '<', 'file1' or die $!;
open my $fh2, '<', 'file2' or die $!;
open my $out, '>', 'matches' or die $!;
while (my $line1 = <$fh1>) {
seek $fh2, 0, 0;
while (my $line2 = <$fh2>) {
if ($line1 eq $line2) {
print $out $line1;
last;
}
}
}
编辑
您的评论已更改了问题。这两个文件都有四行记录,您想比较两个文件中相应记录中的第二行。
Your comment has changed the problem. Both files have four-line records and you want to compare the second line in corresponding records across the two files.
use strict;
use warnings;
open my $fh1, '<', 'file1' or die $!;
open my $fh2, '<', 'file2' or die $!;
open my $match, '>', 'matches' or die $!;
open my $nomatch, '>', 'nomatch' or die $!;
while (1) {
my (@data1, @data2);
for (1 .. 4) {
my $line;
$line = <$fh1>;
push @data1, $line if defined $line;
$line = <$fh2>;
push @data2, $line if defined $line;
}
last unless @data1 == 4 and @data2 == 4;
if ($data1[1] eq $data2[1]) {
print $match @data2;
}
else {
print $nomatch @data2;
}
}
这篇关于Perl while循环和读取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!