#!/usr/bin/gawk -f
# ___  _  _ ____ ___ ___ ____ ___  ____ 
# |  \ |  | |     |   |  |__| |__] |___ 
# |__/ |__| |___  |   |  |  | |    |___ 
#
# The scripts were written to be usefull in
# a research enviornment, but anyone is welcome
# to use them.  Happy awking.  -Tim Sherwood
#
# arrayify.awk
# Perhaps the most useful script in the bunch, this takes a list of data
# (as can be easily created by greping though all of your data files)
# and puts it into a big array (from which you can easily make tables and graphs.
# It's pretty simple, but a very versatile tool.

#####################
BEGIN {
	name = "THISAINTGONNAMATCH";
	num_names = 0;
	num_stats = 0;
	FS = ":"
}

#####################
{
	if ( name!=$1 ){
		name = $1;
		if ( !(name in name_in) ) {
			name_array[num_names] = name;
			name_in[name] = 1;
			num_names++;
		}
	}

	if ( !($2 in stat_names) ){
		stat_names[$2] = 1;
		stat_list[num_stats]  = $2;
		num_stats++;
	}

	stat_array[name,$2] = $3;
}

#####################
END {
	###print the header
	printf "name";
	for( j=0; j<num_stats; j++ ) 
	{
		printf ":%s", stat_list[j];
	}
	printf "\n";

	##print the data
	for( i=0; i<num_names; i++ )
	{
		printf "%s", name_array[i];
		for( j=0; j<num_stats; j++ ) 
		{
			printf ":%s", stat_array[name_array[i],stat_list[j]];
		}
		printf "\n";
	}
}