找到给定范围内的数字?

时间:2011-02-25 06:34:49

标签: arrays perl hash numbers

请告诉我这个概念,写一个Perl程序吗?

167 GATCAAAATACTTGCTGGA 185
192 TAGTAGATAGATAGATAGTAGTAG 228

fileA 中,我的范围是167 to 185,如上所述,还有192 to 228

在另一个 fileB 我有一组数字

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

现在从文件B中的上述数字组中,我需要找出167到185之间存在的数字和 在输出中打印这些数字。

所以,输出将为168,169,179,185, 193

编写这个程序背后的概念是什么?

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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

答案 2 :(得分:0)

如果你可以使用Ruby(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