Jamis
Jamis

Reputation: 55

Finding the numbers in a given range?

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?

Upvotes: 1

Views: 246

Answers (3)

Nikhil Jain
Nikhil Jain

Reputation: 8332

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

Upvotes: 1

sid_com
sid_com

Reputation: 25117

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;
}

Upvotes: 2

kurumi
kurumi

Reputation: 25599

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

Upvotes: -1

Related Questions