
\dbsp
\lstset{language=awk}
\lstset{backgroundcolor=\color{listinggray}}
\lstset{frameround=tttt}
\lstset{basicstyle=\tiny}
\lstset{showstringspaces=false, showspaces=false, showtabs=false}
\begin{center}
\ssp
\begin{lstlisting}[frame=trBL]{}
#!/usr/bin/gawk -f
# /* vim: set filetype=awk : */ -*- awk -*- 
# Original Script by Dr. Tim Menzies as nbins
# Modified for use as tenbins by DJ Boland, January 2007
### patterns0: initialization stuff
 BEGIN{
   BinLog=0; # if non-zero, use bin logging after rounding numbers 
   Bins=10; 
   FS=OFS=","; 
   IGNORECASE=1; 
   Inf=10**32;
 }
             {sub(/\%.*/,"")} 
/^[ \t]*$/   {next}

/@relation/  { Header=1; Data=0; Attr=0; }
/@data/      { Header=0; Data=1; }
/@attribute/ { Attr++; }

Pass==1 && /@attribute/ && (/numeric/ || /real/ || /integer/) {
   Numeric[Attr]= 1;
   Max[Attr]    = -1*Inf;
   Min[Attr]    = Inf;
}
Pass==1 && Data && NF > 1{
   for(I in Numeric) {
     if ( $I ~  /?/ ) continue;
     if ($I > Max[I] ) Max[I]=$I;
     if ($I < Min[I] ) Min[I]=$I;
	 Seen[I,$I]++;
	}
 }
Pass==2 && /@relation/ { 
   for(I in Numeric) {
     Div[I]=Bins
     Bin[I]=(Max[I]-Min[I])/ Div[I];
     print "% attribute:" I "=-=min:"Min[I]  "=-=max:"Max[I] \
                            "=-=bins:"Div[I] "=-=steps:"Bin[I]}
}

Pass==2 && /@attribute/ {
	if  ( (Attr in Numeric) && Attr != MaxAttr ) { 
		Names="";
		for(I=2;I<=Div[Attr];I++) Names=Names",_"I
			sub(/integer|numeric|real/,"{_1"Names"}"); 
	}
}
Pass==2 && Data && NF> 1{ 
   for(I in Numeric) {
     if ( I == MaxAttr ) continue;
     if ( $I ~ /?/ )     continue;
     $I="_"label(I,$I)};
 }
Pass==2 {print $0}

function round(x) { return int(x + (x<0 ? -0.5 : 0.5)) }
function label(i,val,   x) {
   if ( Bin[i]==0 ) return 1;
   x=round((val-Min[i])/Bin[i]);
   if (x==0) {return 1} else {return x};
 }
\end{lstlisting}
\end{center}

\ssp
