Ok, disclaimer: I DON'T think is a good solution, but I wanted to try
it-- it IS Friday after all.

The code below creates a four-dimensional array for all the numbers and
letters in a QWERTY keyboard. It then creates a struct of all the
letters and numbers to provide a "lookup" table to quickly find what row
and position a given letter is in.  Then it finds all its neighbors.

I'm pretty convinced that it would be easier and faster just to
hard-code all the combinations, but it was a fun proof of concept.
Also, I'm REALLY sorry, but  I don't have CF8 on my dev box so I can't
do implicit array and struct declarations.  :(

Passing in "s" to the neighbors function returns the following letters:
w,e,a,d,z,x

<cfscript>
keyboard = arraynew(1);

number_row = arraynew(1);
top_row = arraynew(1);
middle_row = arraynew(1);
bottom_row = arraynew(1);


number_row[1] = '1';
number_row[2] = '2';
number_row[3] = '3';
number_row[4] = '4';
number_row[5] = '5';
number_row[6] = '6';
number_row[7] = '7';
number_row[8] = '8';
number_row[9] = '9';
number_row[10] = '0';

top_row[1] = 'q';
top_row[2] = 'w';
top_row[3] = 'e';
top_row[4] = 'r';
top_row[5] = 't';
top_row[6] = 'y';
top_row[7] = 'u';
top_row[8] = 'i';
top_row[9] = 'o';
top_row[10] = 'p';

middle_row[1] = 'a';
middle_row[2] = 's';
middle_row[3] = 'd';
middle_row[4] = 'f';
middle_row[5] = 'g';
middle_row[6] = 'h';
middle_row[7] = 'j';
middle_row[8] = 'k';
middle_row[9] = 'l';
middle_row[10] = ';';

bottom_row[1] = 'z';
bottom_row[2] = 'x';
bottom_row[3] = 'c';
bottom_row[4] = 'v';
bottom_row[5] = 'b';
bottom_row[6] = 'n';
bottom_row[7] = 'm';
bottom_row[8] = ',';
bottom_row[9] = '.';
bottom_row[10] = '/';

keyboard[1] = number_row;
keyboard[2] = top_row;
keyboard[3] = middle_row;
keyboard[4] = bottom_row;

location_key = structnew();
location_key['1'] = '1,1';
location_key['2'] = '1,2';
location_key['3'] = '1,3';
location_key['4'] = '1,4';
location_key['5'] = '1,5';
location_key['6'] = '1,6';
location_key['7'] = '1,7';
location_key['8'] = '1,8';
location_key['9'] = '1,9';
location_key['0'] = '1,10';

location_key['q'] = '2,1';
location_key['w'] = '2,2';
location_key['e'] = '2,3';
location_key['r'] = '2,4';
location_key['t'] = '2,5';
location_key['y'] = '2,6';
location_key['u'] = '2,7';
location_key['i'] = '2,8';
location_key['o'] = '2,9';
location_key['p'] = '2,10';

location_key['a'] = '3,1';
location_key['s'] = '3,2';
location_key['d'] = '3,3';
location_key['f'] = '3,4';
location_key['g'] = '3,5';
location_key['h'] = '3,6';
location_key['j'] = '3,7';
location_key['k'] = '3,8';
location_key['l'] = '3,9';
location_key[';'] = '3,10';

location_key['z'] = '4,1';
location_key['x'] = '4,2';
location_key['c'] = '4,3';
location_key['v'] = '4,4';
location_key['b'] = '4,5';
location_key['n'] = '4,6';
location_key['m'] = '4,7';
location_key[','] = '4,8';
location_key['.'] = '4,9';
location_key['.'] = '4,0';

function neighbors(key)
        {
                var row = listfirst(location_key[key]);
                var pos = listlast(location_key[key]);
                var my_neighbors= arraynew(1);
                if(row gt 1)
                        {
                                my_neightbors =
arrayappend(my_neighbors,keyboard[row-1][pos]);
                                if(pos lt 10) my_neightbors =
arrayappend(my_neighbors,keyboard[row-1][pos+1]);
                        }
                if(pos gt 1) my_neightbors =
arrayappend(my_neighbors,keyboard[row][pos-1]);
                if(pos lt 10) my_neightbors =
arrayappend(my_neighbors,keyboard[row][pos+1]);
                if(row lt 4)
                        {
                                if(pos gt 1) my_neightbors =
arrayappend(my_neighbors,keyboard[row+1][pos-1]);
                                my_neightbors =
arrayappend(my_neighbors,keyboard[row+1][pos]);
                        }
                return my_neighbors; 
        }

</cfscript>

<cfdump var="#neighbors('s')#">

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;192386516;25150098;k

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:305468
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to