Reputation: 1925
I want to print all lines that are within a specified range. The test code I have so far is:
#!/usr/bin/perl
use warnings;
use strict;
my $start = "25823";
my $end = "35841";
while (<DATA>) {
if ( /(\S+)?\t(\d+)?\t(\d+)?/ ) {
my $feat = $1;
my $datastart = $2;
my $dataend = $3;
if ( $datastart >= $start && $dataend <= $end ) { print }
}
}
__DATA__
A 23594 24158
W 25646 25978
X 26189 26770
Y 31694 33466
Z 34568 36125
B 37516 38912
but this only prints lines X and Y, which are within the range but in an exclusive form. However I would like to get all inclusive lines so that the output would be W, X, Y and Z. How can that be accomplished?
Thanks in advance.
Upvotes: 0
Views: 86
Reputation: 35198
If you want overlapping ranges (instead of just total inclusion), you'll have to expand your IF condition.
Note: You'll have to decide if you want either <
or <=
below, as I don't know your spec.
#!/usr/bin/perl
use warnings;
use strict;
my $start = "25823";
my $end = "35841";
while (<DATA>) {
chomp;
my ($feat, $datastart, $dataend) = split "\t";
if (grep {$start < $_ && $_ < $end} ($datastart, $dataend) ) {
print "$_\n"
}
}
__DATA__
A 23594 24158
W 25646 25978
X 26189 26770
Y 31694 33466
Z 34568 36125
B 37516 38912
Outputs:
W 25646 25978
X 26189 26770
Y 31694 33466
Z 34568 36125
Upvotes: 2