#!/usr/local/bin/perl # transpose. # reads in a file containing a list of floats in ascii # prints out its transpose, with the rows figured out by reading the .d file # or from the given integer column argument. # If neither is given then the count of the first line is used. # # usage # transpose.p weights # transpose.p dh=1 weights (reads weights.d) # transpose.p rows=4 20 20 20 1 weights # # success of this program verified thus: # transpose.p dh=1 zeros=1 gt42 | hintond.p rows=3 20 $dh=0; $dhfile=""; $spacer = "\t" ; $spacer = "" ; $rows=0; # number of rows (is calculated from dh if dh is given) $cols=0; # $zeros=0; # whether to include entries "0" for all the places where dh > 1 # so that zero weights are implicit. $returns=0 ; # whether and when to insert an extra return into each projective # field # How to override min and max values with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $l=0; while ( $ARGV[0]=~ /^(\d+)/ ) { $num[$l++]=$1; if($1 > $cols ) {$cols = $1; } shift; } $lmax = $l ; # what remains in argv is used as filenames for input if ( $dhfile ne "" ) { $dh = 1 ; } elsif ( $dh == 1 ) { # there had better be another ARGV, the weight file, to which .d is added. $dhfile = $ARGV[0].".d" ; } if ( $dh ) { print STDERR "going to read dh from $dhfile\n" ; open ( DH , "<$dhfile" ) ; @deltas = ; # reads in the whole lot $joined = join ( ' ', @deltas ) ; $num_of_rows = 0 ; @dhs = split ( /[ \t\n]+/ , $joined ) ; foreach $arg ( @dhs ) { if ( $arg =~ /\b0\b/ ) { $num_of_rows ++ ; } } if ( !$rows ) { $rows = $num_of_rows ; } $K = $#dhs + 1 ; print STDERR "read in dh array - $K weights in $num_of_rows rows\n" ; close ( DH ) ; } if ( !$rows ) { print STDERR "Warning, no rows given on command line or in dhfile\n" ; } if ( ! ( !$cols && ($dh == 0 ) ) ) { $/ = ""; # read in by the paragraph $* = 1 ; } $l=0; $thiscol=0; $currentrow = 1 ; $k = 0 ; # reserved name for the current weight number if ( ! ( $_ = <> ) ) { print STDERR "No input\n" ; exit ( 0 ) ; } do { s/^\s*//; # get rid of initial gap on line @fields=split ; if ( !$cols && ($dh == 0 ) ) { $cols = $#fields + 1 ; $num[$l] = $cols ; $lmax = $l ; $/ = ""; # read in by the paragraph from now on $* = 1 ; } if ( $#fields >= 0 ) { for ( $i = 0 ; $i <= $#fields; $i++) { if ( $zeros ) { # need to insert zeroes into $w ? while ( $wsrow[$thiscol] < $currentrow - 1 ) { $wsrow[$thiscol] ++ ; $w[$thiscol] .= "0\t" ; if ( $returns && ( $wsrow[$thiscol] % $returns == 0 ) ) { $w[$thiscol] .= "\n" ; } } $wsrow[$thiscol] ++ ; } $w[$thiscol] .= $fields[$i].$spacer ; if ( $returns && ( $currentrow % $returns == 0 ) ) { $w[$thiscol] .= "\n" ; } if ( $dh == 0 ) { $thiscol++; while($thiscol==@num[$l]){ &end_of_row ( ) ; } } else { # the dhs determine the number in each row, and the spaces. if ( $dhs[$k] == 0 ) { &end_of_row ( ) ; } else { $thiscol += $dhs[$k] ; } $k ++ ; } if( $rows && ( $currentrow > $rows ) ) { last; } } } # print "\n"; } while( ( $_ = <> ) && ( !$rows || ( $currentrow <= $rows) ) ) ; $currentrow -- ; if ( $zeros ) { # might need to add a string of zeros to each $w. for ( $c = 0 ; $c < $cols ; $c ++ ) { while ( $wsrow[$c] < $currentrow ) { $wsrow[$c] ++ ; $w[$c] .= "0\t" ; if ( $returns && ( $wsrow[$c] % $returns == 0 ) ) { $w[$c] .= "\n" ; } } } } print STDERR "# number of rows in this file = ", $cols , "\n" ; for ( $c = 0 ; $c < $cols ; $c ++ ) { print $w[$c] , "\n" ; } sub end_of_row { $l++; $y += $h ; $currentrow ++ ; if($l>=$lmax){$l=0;} if ( $dh && $thiscol + 1 > $cols ) { $cols = $thiscol + 1 ; } $thiscol=0; }