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

I have a file containing positions, length and text, in paragraph format. eg first filed is position, the number in the second field is the length of the text in the forth field, the third field is the name.
0 10 ab hellothere 4 5 ac notimportant 2 3 ad notimportant 10 23 ab hellloagainhellloagainnn 4 5 ac notimportantnotimportant 2 3 ad notimportantnotimportant
I have another file containing positions of paragraph text I need to retain, relative to the top line (ab) of the paragraph
0 20 40 60 100 120
so the first line says to keep the text up to position 20 (keeping paragraph structure)
hellothere notimportant notimportant hellloagainhellloagai notimportantnotimport notimportantnotimport
Then I would need to keep the text from position 40 to 60, and so on. So I need to filter the text based on the positions in the second file (which gives the co-ordinates of the filtered text relative to the first line in the paragraph). Hopefully this makes sense. I am already reading in the data 1 paragraph at a time and processing the resulting text. the paragraph text and names is read into an hash
my %seqs foreach my $sline (@slines){ my($start,$size,$name,$text) = split /\s+/, $sline; $seqs{$name} = $text; }
I was hoping somebody may have an idea for some kind of filter before it is put into the hash, the tricky bit for me is using the info in the second file. Many thanks!

Replies are listed 'Best First'.
Re: filter one file based on another
by NetWallah (Canon) on Aug 15, 2008 at 01:09 UTC
    The documentation for the "substr" function may be what you are looking for.

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

Re: filter one file based on another
by GrandFather (Saint) on Aug 15, 2008 at 00:27 UTC

    It doesn't make sense - at least to me. How about a little more data so we can see the result of the second line kicking in and data that is a little more real.

    A description of why you are doing this may help too.

    What is the significance of the 'position' information and what constitutes 'length'?


    Perl reduces RSI - it saves typing
Re: filter one file based on another
by graff (Chancellor) on Aug 16, 2008 at 01:47 UTC
    The part that is confusing (to me, at least) is: what do you mean when you say:

    Then I would need to keep the text from position 40 to 60, and so on.

    Are you supposed to produce a distinct output for each line (each set of positions) in the second file (e.g. open one output file for "0 20", then another output file for "40 60", etc)? If not that, then what do you mean by "keep"? Give us a little more of the "big picture"...

      Okay, sorry I was trying to keep it succinct. Thanks for the interest. The original data is multiple alignments of DNA sequences, in blocks- 4 lines to a block corresponding to 4 different species. Each line contains the position of the sequences (and also other descriptives in the first few fields). The last field is the sequence. So by 'keep' I mean dont filter/remove and place that block into the hash I described earlier, so I can then process elsewhere in the program. The filtering is based on the second file containing positions of the first sequence in the blocks (the reference sequence) which I need to 'keep'. Is this more clear?