Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The problem is that your $placeholder value isn't set back to 0 when you finish testing a range, so you never go back to test the previous lines. The solution to your problem would be to add a $placeholder = 0 at the end of your foreach loop.

Now, if you want some advice on your code, first, I would have gone the other way around, have all the ranges in memory, and for each line of your data, test all ranges. And to keep track of the file that needs to be written on, an array of hashes could do the trick. Something like :

[ { start => 1, end => 1001, file => *FH1 }, { start => 500, end => 2001, file => *FH2 } ]
Except the content would have been filled by perl instead of by hand like I just did :) . Then the algorithm would have been something like :
for each line $position = getPosition() for $range in @ranges print $range->{FH} $line if (($position > $range->{start}) && ($ +position < $range->{end}))

Then, maybe you don't want to rewrite your whole code (that's why I didn't bother much), still, there are some useful things you might like to know. If you want to have a "do something for each value and stop when condition" construct in Perl, you can use the last keyword inside a foreach loop. In your case that would be :

SNP:for my $snp (@SNPs) { my @get_SNPs = split(/\t/, $snp); my $position = $get_SNPs[3]; last SNP if ($position > $end); # stop reading, we're out of r +ange ! if (($position >= $start) && ($position <= $end)) { print OUT "@get_SNPs"; } }

Also, instead of

@array = split /\t/, $string; my $v1 = $array[1]; my $v2 = $array[2];
you can simply write my (undef, $v1, $v2) = split /\t/, $string;


In reply to Re: Sorting Data By Overlapping Intervals by Eily
in thread Sorting Data By Overlapping Intervals by ccelt09

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found