Saturday, October 31, 2009

AWK dictionary, second version

A newer version of the 'awk dictionary' program: you can store string pairs, and ask for any of them. The program will give you back its pair. (In this version it does not matter, that the questioned string is the first or second one of the pair.)



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;
b[$2] = $1;
break
} else if(NF == 1)
{
printf $1 " ";
print a[$1] b[$1];
break
}
} while (input > 0)
}



A sample output:
D:\Awk>awk talk
prompt>He Helium
prompt>Ne Neon
prompt>He
He Helium
prompt>Neon
Neon Ne
prompt>exit
Exit!

No comments: