I have two files myresult and annotation. data in the two files appear to be as range but they are not, that\'s why i cant store it in an array and i need to use split operator
You don't need to iterate over both the arrays. If the starting points of the ranges are ascending, you can use the following code:
#!/usr/bin/perl
use warnings;
use strict;
my @result = qw( 4..20 8..12 14..22 22..29 27..29 28..35 40..50 );
my @annot = qw( 1..5 11..13 25..37 45..55 );
my $from = (split /\.\./, $result[0])[0];
my $to = (split /\.\./, $result[-1])[1];
for my $i ($from .. $to) {
print "$i\n" if grep inside($i, $_), @result
and grep inside($i, $_), @annot;
}
sub inside {
my ($i, $range) = @_;
my ($from, $to) = split /\.\./, $range;
return ($from <= $i and $i <= $to)
}
Translate each data set into an array of values.
Then use a hash to count the matched uniq values from both lists:
#!/usr/bin/perl -w
use strict;
use warnings;
use autodie;
use List::MoreUtils qw(uniq);
my @result = do {
#open my $fh, '<', "myresult";
open my $fh, '<', \ "0..351\n12..363\n24..375\n36..387\n48..399\n60..411\n";
map { my ( $min, $max ) = /\d+/g; ( $min .. $max ) } <$fh>;
};
my @annot = do {
#open my $fh, '<', "myresult";
open my $fh, '<', \ "272..1042\n1649..2629\n3436..4752\n4793..4975\n5408..6022\n6025..6252\n";
map { my ( $min, $max ) = /\d+/g; ( $min .. $max ) } <$fh>;
};
my %count;
$count{$_}++ for uniq(@result), uniq(@annot);
print join( ' ', sort { $a <=> $b } grep { $count{$_} == 2 } keys %count ), "\n";
Outputs:
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411