Thursday, October 15, 2009

AWK dictionary

Another really simple awk program, just a mutation from the previous post. It works like a dictionary, if you put two words in it, it stores the pair. If you type only one word: it will search for a match.

BEGIN {
ARGC = 2; ARGV[1] = "-"
do {
printf("prompt>")
while ((input = getline) > 0)
if ($0 ~ "exit") {
print "Exit!"
input = 0;
break
} else if(NF > 1)
{
a[$1] = $2;
break
} else if(NF == 1)
{
print a[$1];
break
}
} while (input > 0)
}

A sample run is here:
prompt>He Helium
prompt>Ne Neon
prompt>He
Helium
prompt>Ne
Neon

No comments: