A new utility for Galaxy written in awk From: dagibbs@quantum.qnx.com (David Gibbs) Date: Tue, 09 Feb 1993 23:34:03 +0000 I have written a new Galaxy utility. This one creates distance cross-references based on a turns results and a list of planets. (Like on maps, for cities.) Syntax is: distance <turn results file> <planet> <planet>* Example: distance Beta_t14_results Blob Enkidu 177 Aquarium Produces the following output: -------- begin output -------------------------- Planet distances are: 1 Blob 0.00 48.65 12.73 28.96 2 Enkidu 48.65 0.00 41.49 59.49 3 177 12.73 41.49 0.00 22.42 4 Aquarium 28.96 59.49 22.42 0.00 1 2 3 4 -------- end output ------------------------------ The printf() calls on lines 88, 91, 94, and 102 can be customized to line things up the way you want it. The above output was generated with tabstops = 4 in mind. Hope somebody finds this useful. -David (dagibbs@quantum.qnx.com) [Ooze in Alpha, Bytor in Beta, Homebodies in Gamma, VOC in game 7] --------------------CUT HERE-------------------------- #!/bin/awk -f BEGIN { # This script takes turn records and a list of planets and generates # a cross-index distance listing for the planets. # # It expect the first argument to be the source file and all subsequent # arguments are planet listings. # # STATES # 1 program start, looking for "Your Planets" line # 2 reading in planets # 3 looking for "Unidentified Planets" or "Uninhabited Planets" line if ( ARGC < 4 ) { print "Recquires at least 3 arguments" print "Takes <input file> <planet> <planet>*" print " " exit } num_plan = ARGC - 2 for( i = 1; i <= num_plan; i++ ) { pl = ARGV[i+1] gsub( " ", "_", pl ) planets[i] = pl fnd[pl] = 0 ARGV[i+1] = "" } state = 1 } (state == 1) && (NF == 2) { if ( $1 == "Your" && $2 == "Planets" ) { getline; getline state = 2 next } } state == 2 { if ( NF == 0 ) { state = 3 next } else if ( $1 in fnd ) { x[$1] = $2 y[$1] = $3 fnd[$1] = 1 } } (state == 3) && (NF == 2) { if ( $2 == "Planets" ) { getline; getline state = 2 next } } END { no_out = 0 for ( i = 1; i <= num_plan; i++ ) { pl = planets[i] if ( fnd[ pl ] ) { no_out++ out_plan[ no_out ] = pl } else { print "Unable to find planet:", pl, "... ignoring it" } } if ( !no_out ) { next } print " " print "Planet distances are: " print " " for ( i = 1; i <= no_out; i++ ) { pli = out_plan[i] xi = x[pli] yi = y[pli] printf( "%2d %-15s ",i, pli ) for ( j = 1; j <= no_out; j++ ) { if ( i < j ) { plj = out_plan[j] xj = x[plj] yj = y[plj] dist[i,j] = sqrt( (xi-xj)*(xi-xj) + (yi-yj)*(yi-yj) ) printf( "%6.2f\t", dist[i,j] ) } else if ( i == j ) { printf( "%6.2f\t", 0 ) } else { printf( "%6.2f\t", dist[j,i] ) } } printf("\n") } print " " printf( "%18s ", " " ) for ( i = 1; i <= no_out; i++ ) { printf( "%3d \t", i ) } printf "\n" printf "\n" } Up