HASH.sml

  Download

More scripts: SML Fundamentals

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# HASH.sml
# Sample script for tutorial Writing Scripts with SML
# demonstrates a use of the HASH class 
clear();
numeric i;  # counter
class STRING key;
class STRINGLIST list;	# stringlist to hold series of letters
numeric countingHash[];	# declare hash of numeric variable to hold count for each letter
# add different numbers of letters a, b, c, and d to the stringlist
list.AddToEnd("a"); list.AddToEnd("a"); list.AddToEnd("a");
list.AddToEnd("b");
list.AddToEnd("c"); list.AddToEnd("c"); list.AddToEnd("c");
list.AddToEnd("d"); list.AddToEnd("d");
# print out the resulting stringlist values separated by commas
printf("stringlist contains: ");
for i = 0 to list.GetNumItems() - 2 {
	printf("%s, ", list.GetString(i) );
	}
printf("%s\n", list.GetString(i-1) );
# use hash to count the frequency of each letter in stringlist 
for i = 0 to list.GetNumItems() - 1 {
	key = list.GetString(i);
	++countingHash[key];
	}
class STRINGLIST keylist;	# list of keys returned from the hash
keylist = countingHash.GetKeys();
for i = 0 to keylist.GetNumItems() -1 {
	key = keylist.GetString(i);
	printf("count for %s = %d\n", key, countingHash[key] );
	}