2011-02-25 179 views
1

請給我一個寫Perl程序的概念嗎?查找給定範圍內的數字?

167 GATCAAAATACTTGCTGGA 185 
192 TAGTAGATAGATAGATAGTAGTAG 228 

的fileA我已經從167 to 185的範圍內所給出如上,並且還192 to 228

在另一個 FILEB

IVE的從上述設定號碼

2 3 4 5 6 7 8 168 169 179 185 193 1000 

的現在文件B中的一組數字,我需要找出哪些數字介於167至185之間, 打印那些麻木在輸出中。

因此,輸出將是168,169,179,185, 193

這將是後面寫這個程序的概念?

回答

1
use strict; 
use warnings; 

open my $fh, '<', $file1 or die "unable to open '$file1' for reading :$!"; 
my @arr1 =(); 
while(my $line = <$fh>){ 
while($line =~ /(\d+).*?(\d+)/gs){ 
    push (@arr1, $1..$2); 
} 
} 
close($fh); 
my @arr2 = qw/2 3 4 5 6 7 8 168 169 179 185 193 1000/; 
my %hash; 
@hash{@arr1} =(); 
for my $num (@arr2){ 
print"$num is present in the list\n" if(exists $hash{$num}); 
} 

輸出:

168 is present in the list 
169 is present in the list 
179 is present in the list 
185 is present in the list 
193 is present in the list 
+0

thanku nikhil我已經得到了答案,但我有很多範圍在fileA。我如何可以爲各種範圍? – Jamis

+0

如果你有不同的範圍,那麼你可以把它作爲'@ arr1 =(1..10,167..185)'; –

0

如果你可以使用紅寶石(1.9+)

#!/usr/bin/env ruby 
fileA=File.read("fileA").split 
s,e = fileA[0] , fileA[-1] 
fileB=File.read("fileB").split 
puts fileB.select {|x| x >= s and x<=e } 

輸出:

$ ruby myRange.rb 
168 
169 
179 
185 
2

如果你有Perl的版本5.010或更大,你可以嘗試噸他:

#!/usr/bin/env perl 
use warnings; 
use 5.010; 

my @arr1 = (167..185); 
my @arr2 = qw/2 3 4 5 6 7 8 168 169 179 185 1000/; 

for my $num (@arr2){ 
    say"$num is present in the list" if $num ~~ @arr1; 
} 
+0

thanku sid ..有一個清晰的想法.. – Jamis