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

Dear monks, I want to chop the beginning and end of lines in 2 files based on a condition.
file1.txt contains >sample1 ACTACTAGCTC >sample2 CGATCGAACC >sample3 CGTTGAACTGAA file2.txt contains >sample1 1 4 23 43 65 67 17 15 1 3 2 >sample2 23 43 65 12 34 54 76 2 9 12 >sample3 32 12 43 54 26 47 58 11 8 6 9 9
The condition to be checked is in the file2.txt, for each sample, if a continous stretch of numbers at the end or beginning is less than 10, only those numbers should be chopped off (the alphabets in the same position in other file(file1.txt) should also be removed). results to be written in a new file. Thank you.

Replies are listed 'Best First'.
Re: chopping the beginning and end of lines in 2 files
by graff (Chancellor) on Nov 13, 2008 at 04:00 UTC
    The way it usually works is: you show us the code you've tried, and explain how the result you got was different from the result you really wanted. This doesn't sound like such a difficult puzzle to work out, so try something, and if it doesn't work, show us.
      #!/usr/bin/perl $b="a b c d e f g h i j"; $a="1 3 45 65 7 89 67 9 8 2"; @seq=split(/ /,$b); @arr=split(/ /,$a); $beg=0; $end=0; for($i=0;$i<scalar(@arr);$i++){ if ($arr[$i]<15){ $beg++; } else{ last; } } @arr_rev=reverse(@arr); for($i=0;$i<scalar(@arr_rev);$i++){ if ($arr_rev[$i]<15){ $end++; } else{ last; } } for ($i=0+$beg;$i<scalar(@arr)-$end;$i++){ print "$seq[$i] "; } print "\n"; for ($i=0+$beg;$i<scalar(@arr)-$end;$i++){ print "$arr[$i] "; }
      this is a sample which i worked it... since there are more than one way to do in perl, i thought i wil ask you. Moreover, i sometimes feel that my codes dont look mature when compared to what you all write. want to learn the way how monks do it:)

        Ah, much better! We don't care what your code looks like (actually, that's a lie) so long as you are prepared to take some advice and improve. This is lovely code - there is a ton of room for improvement. ;)

        First off, always use strictures (use strict; use warnings;). Don't use $a and $b as variables - they are special (used by sort). Avoid the C style for loop - Perl's for loop is safer, clearer and cleaner.

        Generally if you have parallel data try to use a single data structure for it so you don't have to double handle everything with the resultant duplication of code and much greater risk of errors.

        Bearing that in mind consider:

        use strict; use warnings; my $str1 = "a b c d e f g h i j"; my $str2 = "1 3 45 65 7 89 67 9 8 2"; my @seq = split (' ', $str1); my @arr = split (' ', $str2); my @data = map {[$seq[$_], $arr[$_]]} 0 .. $#seq; my $beg = 0; my $end = 0; for (@data) { last unless $_->[1] < 15; $beg++; } for (reverse @data) { last unless $_->[1] < 15; $end++; } print join (' ', map {$data[$_][0]} $beg .. $#arr - $end), "\n"; print join (' ', map {$data[$_][1]} $beg .. $#arr - $end), "\n";

        Perl reduces RSI - it saves typing

        I hope a few general remarks are welcome.

        I would suggest you to use use strict; and use warnings;.

        And please don't use $a and $b, the are even with use strict; not under the strict conditions, because there are used by sort.


        Edit: I should renew the page before answering. Grandfather was faster and he did better.

        I kind of like this one:

        [...] for ($start=0;$arr[$start]<15;++$start) {}; for ($end=$#arr;$arr[$end]<15;--$end) {}; print "@seq[$start..$end]\n"; print "@arr[$start..$end]\n";

        Just don't understand if you were looking for a number smaller than 10 or 15 :)


        GreetZ!,
          ChOas

        print "profeth still\n" if /bird|devil/;
Re: chopping the beginning and end of lines in 2 files
by GrandFather (Saint) on Nov 13, 2008 at 03:57 UTC

    I don't see what you have tried or where you are having trouble. Surely you don't expect that we are just going to sit down and write a complete solution to your problem from scratch given a poor problem specification and not even an example of the expected output?

    PerlMonks is a Perl language support site, not a free coding site. We will be delighted to help you resolve issues in your code, but generally are less helpful when it comes to doing your job for you - although you ought to have figured that out by now!


    Perl reduces RSI - it saves typing