#!/usr/local/bin/perl -w # selectraw2A.p # # Used in qecc/CYCLIC # # reads a raw 0/1 ascii file and selects SOME # rows from it, writing the rejected rows to stdout. # Rows are rejected if they would take us over the column weight limit, # or if we have accepted enough rows. # # The rejected stuff at stdout is what I am actually interested in. # # selectraw2A.p M=500 < string/C2003b > string/C2003b1500 # $colweightlimit = 2 ; $N = 0 ; $M = 500 ; # target number of rows to ACCEPT # $verbose = 0 ; $addspaces=0 ; # to add spaces between chars eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; print STDERR "===== selectraw2A.p ===== (c) DJCM Nov 1998 =====\n" ; $m = 0 ; $rejected = 0 ; READLOOP: while(<>) { # check to see if we have accepted all we need if ($m >= $M ) { print ; # write to STDOUT $rejected ++ ; next READLOOP ; } if ($addspaces ) { # assumes no white space at all. s/\n// ; # remove the new line char @a = split ( '' , $_ ) ; } else { @a = split ; } if ( $#a > 2 ) { # automated ignoring of P1 header. # first check to see if any cols are full $n = 0 ; foreach $b ( @a ) { $n ++ ; if ( $b ) { if ( $numnlist[$n] && ($numnlist[$n] >= $colweightlimit) ) { # rejected row! s/\n// ; # remove the new line char print ; # write to STDOUT print "\n" ; $rejected ++ ; next READLOOP ; } } } ## OK we have decided to accept this row. $m ++ ; $n = 0 ; foreach $b ( @a ) { $n ++ ; if ( $b ) { &mnadd($m,$n); } } } else { print STDERR "ignoring blank line" ; } } $N = $n ; # $M = $m ; print STDERR "done N = $n ; M = $m\n" ; print STDERR "passed on $rejected lines to stdout\n" ; if ( 0 ) { $head = "$N $M\n" ; print STDERR "Writing code.\n" ; # find $t (max) and $tr (max) $t = 0 ; $tr = 0 ; for ( $n = 1 ; $n <= $N ; $n ++ ) { if ( $numnlist[$n] > $t ) { $t = $numnlist[$n] ; } } for ( $m = 1 ; $m <= $M ; $m ++ ) { if ( $nummlist[$m] > $tr ) { $tr = $nummlist[$m] ; } } $head .= "$t $tr\n" ; # append zeros to short lines for ( $n = 1 ; $n <= $N ; $n ++ ) { $thisn = $numnlist[$n] ; $head .= "$thisn " ; while ( $thisn < $t ) { $nlist[$n] .= "0\t" ; $thisn ++ ; } } $head .= "\n" ; for ( $m = 1 ; $m <= $M ; $m ++ ) { $thism = $nummlist[$m] ; $head .= "$thism " ; while ( $thism < $tr ) { $mlist[$m] .= "0\t" ; $thism ++ ; } } $head .= "\n" ; print $head ; # finish lists for ( $n = 1 ; $n <= $N ; $n ++ ) { $nlist[$n] =~ s/\t$/\n/ ; print $nlist[$n] ; } for ( $m = 1 ; $m <= $M ; $m ++ ) { $mlist[$m] =~ s/\t$/\n/ ; print $mlist[$m] ; } } sub mnadd { local($m,$n)=@_; # put m and n on each others lists $nlist[$n] .= $m."\t" ; $mlist[$m] .= $n."\t" ; $numnlist[$n] ++ ; $nummlist[$m] ++ ; }