#!/usr/local/bin/perl -w # raw2A.p # reads a raw 0/1 ascii file and writes an alist matrix # raw2A.p < DSC.pcm > DSC.273.82.A # # the inverse operation is performed by A2H.p, see also A2dat.p # $N = 0 ; $M = 0 ; # $verbose = 0 ; $addspaces=0 ; # to add spaces between chars eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; print STDERR "===== raw2A.p ===== (c) DJCM Nov 1998 =====\n" ; $m = 0 ; while(<>) { if ($addspaces ) { # assumes no white space at all. s/\n// ; # remove the new line char @a = split ( '' , $_ ) ; } else { @a = split ; } if ( $#a > 2 ) { $m ++ ; $n = 0 ; foreach $b ( @a ) { $n ++ ; if ( $b ) { &mnadd($m,$n); } } } else { print "ignoring blank line" ; } } $N = $n ; $M = $m ; print STDERR "done N = $n ; M = $m\n" ; if ( 1 ) { $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] ++ ; }