perl - How can I use the until function with appropriate way -
i have file want filter that:
##matrix=axtchain 16 91,-114,-31,-123,-114,100,-125,-31,-31,-125,100,-114,-123,-31,-114,91 ##gappenalties=axtchain o=400 e=30 chain 21455232 chr20 14302601 + 37457 14119338 chr22 14786829 + 3573 14759345 1 189 159 123 24 30 22 165 21 20 231 105 0 171 17 19 261 0 2231 222 2 0 253 56 48 chain 164224 chr20 14302601 + 1105938 1125118 chr22 14786829 + 1081744 1100586 8 221 352 334 24 100 112 34 56 56 26 50 47 ……………………. chain 143824 chr20 14302601 + 1105938 1125118 chr22 14786829 + 1081744 1100586 8
so, briefly,there blocks separated blank line. each block begins line " chain xxxxx " , continues lines numbers. want filter out file , keep blocks chain , number follows greater 3000. wrote following script that:
#!/usr/bin/perl use strict; use warnings; use posix; $chain = $argv[0]; #it filters chains chains >= 3000. open $chain_file, $chain or die "could not open $chain: $!"; @array; while( $cline = <$chain_file>) { #next if /^\s*#/; chomp $cline; #my @lines = split (/ /, $cline); if ($cline =~/^chain/) { @lines = split (/\s/, $cline); if ($lines[1] >= 3000) { #print $lines[1]; #my @lines = split (/ /, $cline); #print "$cline\n"; push (@array, $cline); } } until ($cline ne ' ') { push (@array, $cline); } foreach (@array) { print "$_\n"; } undef(@array); }
the problem can print headers (chain xxxxx…..) , not numbers follows @ next lines of each block. i'm using until function till find blank line, doesn't work. if me that…. thank in advance, vasilis.
the first problem here ' '
single space, not blank line (""
or ''
should fine since you've chomp
-ed line.
the second problem that
until ( $cline ne "" )
is same as
while ( $cline eq "" )
which opposite of need push lines @array
.
that said, flip-flop operator more suitable construct you're after:
my @array; while ( <$chain_file> ) { # using $_ instead of $cline chomp; if ( { /^chain\s+(\d+)/ && $1 >= 3000 } .. /^$/ ) { # accumulate lines in @array push @array, $_; # false until lhs evaluates true ... } # ... true until rhs evaluates true else { ( @array ) { print $_, "\n"; # print matches } @array = (); # reset/clear out @array } }
Comments
Post a Comment