abhikalrt53 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have started to learn perl but now I am completely stuck in this problem, thus I seek your help.

I have seven files with different number ranges. I want to compare their ranges and detect the common range from them. Below I have shown an example with three files (file1.txt, file2.txt anf file3.txt). These files are like:

file1.txt:

68476204: 9-50, 55-75, 80-132 NC_23987: 2-22, 1001-1085 68473073: 1-8 68485121: 1-10, 20-55

file2.txt:

68485121: 15-45 45905121: 2-98, 201-255 68476204: 8-30, 57-77, 88-180 NC_23987: 1-18, 1021-1055 68473073: 14-44

file3.txt:

68485121: 16-42 68476204: 8-22, 55-76, 81-118

From here, I want to generate two output. First one is the common ranges (common in all three) after matching left column value(which are common in all three of them). For the above input, my output1.txt will be:

68485121: 20-42 68476204: 9-22, 57-75, 88-118

The second output (output2.txt) contain only those ranges those are >=15. Here, the output2.txt will be:

68485121: 20-42 68476204: 57-75, 88-118

Any type of suggestion is appreciated.

Thanks

Replies are listed 'Best First'.
Re: Intermediate range calculation from files
by BrowserUk (Patriarch) on Aug 31, 2016 at 08:31 UTC
    Any type of suggestion is appreciated.

    You've described the task very clearly; what you've omitted to describe is which part of the problem you are having a problem with?

    • Reading the files?
    • Parsing their content?
    • Accumulating the ranges?
    • Deciding what goes in which file?
    • Writing the output?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Intermediate range calculation from files
by choroba (Cardinal) on Aug 31, 2016 at 12:47 UTC
    It isn't clear how to handle intervals like 42-42 - can they appear in the input? Can they appear in the output?

    The following code first organizes the intervals into %changes where the beginning and end of each interval are the main keys, i.e. you can walk the keys and see all the points where the situation changes (hence the name).

    The second group of loops walks the "changes", and records how many intervals are currently "active", in %current . If the number of current intervals of the same id is the same as the number of input files, the point is stored in %intervals as the starting point; and similarly for the end points.

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my %changes; for my $file (@ARGV) { open my $in, '<', $file or die "$file: $!"; while (<$in>) { chomp; my ($id, $intervals_string) = split /: /; my @intervals = map [ split /-/ ], split /, /, $intervals_stri +ng; for my $interval (@intervals) { my ($from, $to) = @$interval; $changes{$from}{$id}{$file} = 'start'; $changes{$to}{$id}{$file} = 'end'; } } } my %current; my %intervals; for my $point (sort { $a <=> $b } keys %changes) { for my $id (keys %{ $changes{$point} }) { for my $file (keys %{ $changes{$point}{$id} }) { if ('start' eq $changes{$point}{$id}{$file}) { $current{$id}{$file} = 1; push @{ $intervals{$id} }, [$point] if @ARGV == keys % +{ $current{$id} }; } else { $intervals{$id}[-1][1] = $point if @ARGV == keys %{ $c +urrent{$id} }; delete $current{$id}{$file}; delete $current{$id} if ! keys %{ $current{$id} }; } } } } for my $id (keys %intervals) { say "$id: ", join ', ', map "$_->[0]-$_->[1]", @{ $intervals{$id} +}; }
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Intermediate range calculation from files
by Anonymous Monk on Aug 31, 2016 at 07:44 UTC
Re: Intermediate range calculation from files
by GotToBTru (Prior) on Aug 31, 2016 at 12:20 UTC

    It's important in homework to show your work. Let's see what you have so far.

    Perhaps more importantly, you need to work out how to solve this. How would you do this with pencil and paper? That would be where I would start.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Intermediate range calculation from files
by haukex (Archbishop) on Sep 02, 2016 at 12:50 UTC