找出给定范围内的数字?

发布于 2024-10-19 07:17:02 字数 458 浏览 3 评论 0原文

请告诉我在这背后编​​写 Perl 程序的概念?

167 GATCAAAATACTTGCTGGA 185
192 TAGTAGATAGATAGATAGTAGTAG 228

文件A中,我的范围是上面给出的167到185

在另一个文件B中 也是192到228 > 我现在已经从文件 B 中的上述数字集中找到了一组数字

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

,我需要找出 167 到 185 范围内存在的数字以及 在输出中打印这些数字。

那么,输出将是 168,169,179,185, 193

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

kindly tel me the concept to write a Perl program behind this ?

167 GATCAAAATACTTGCTGGA 185
192 TAGTAGATAGATAGATAGTAGTAG 228

in a fileA i ve a range from 167 to 185 as given as above and also 192 to 228

in another fileB i ve set of numbers

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

now from the above set of numbers in file B, i need to find out which are the numbers present between the range of 167 to 185 and
print those numbers in the output.

so, output will be 168,169,179,185, 193

what will be the concept behind writing this program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

动听の歌 2024-10-26 07:17:02

如果您有 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;
}

If you have Perl-version 5.010 or greater you could try this:

#!/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;
}
欢烬 2024-10-26 07:17:02
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
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});
}

Output:

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
海的爱人是光 2024-10-26 07:17:02

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

if you can use 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 }

output:

$ ruby myRange.rb
168
169
179
185
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文