PDA

View Full Version : best way to search...



danhodkinson
March 7th, 2008, 11:09 AM
hi guys, i am unsure of which route to take here...

i have a input box where the user can type a search term and if it is a hit e.g. ball then my pointer is shown at the appropriate x and y co-ordinates.

i have about 50different search terms but some have more than one hit e.g. searching for the terms sports, squash, tennis & gym will all point to the same place.

i was about to set up a big if (search == sport || search == gym etc) with about 50 different else if's, but i thought there may be a better way to go about it.

would it be more convenient to do it with xml, or arrays?? i am unsure at this point which way to go and dont want to venture off on something when there is a much quicker, practical and less memory hungry route??

thanks for any advice.
Dan

danhodkinson
March 11th, 2008, 12:14 PM
anybody have any advice?
thanks

Charleh
March 11th, 2008, 12:54 PM
I'd add an array member to your objects at run time and fill the array with the terms which each item can be searched on - then when the user searches just loop through all the child objects on the particular part of the stage which you will be searching on and check the array for matching values - you'll have to add the arrays manually during onLoad but it should be easier in the long run - it depends how you've got your code setup

dzhedzho
March 11th, 2008, 01:21 PM
Please explain the end result you are attempting to achieve. From what I understand the search will be only by one key and the result will be a DisplayObject instance; What you can do is do is make a hash.



var hash:Object = {term:target, sports:ball,squash:ball,tennis:ball,gym:ball, rabbit:carrot, vegetable:carrot}
function updatePointer (search:String) {
if(hash[search]!=null){
pointer.x=hash[search].x;
pointer.y=hash[search].y;
}else{
//term not in the hash, or the target clip missing
}
}
If you search by more than one key you are better off using XML imo.

Anogar
March 11th, 2008, 04:49 PM
This is a good time to use a switch / case , like the following:



switch(searchString)
{
case "banana" :
cursorGoto(50, 50);
break;
case "cheese" :
cursorGoto(50, 100);
break;
case "sports" :
case "ball" :
case "squash" :
case "tennis" :
case "gym" :
cursorGoto(10, 10);
break;
case "penguin"
cursorGoto(20, 20);
break;
default :
cursorGoto(0, 0);
}

danhodkinson
March 13th, 2008, 12:28 PM
thanks anogar, i completely fogot about switches,
however with my pointer i am using i have to supply the .x and .y values on 2 seperate lines, how would i get it to use (x,y) like in your example?
thanks
Dan

Anogar
March 13th, 2008, 03:11 PM
You would make a function like this:



function cursorGoto(cx:Number, cy:Number):void
{
myCursor.x = cx;
myCursor.y = cy;
}


Now you just call cursorGoto, and you'll just have one line instead of two in each place except the function itself.

Battie
March 14th, 2008, 07:44 AM
Hmm.

Are your coordinates following a pattern that can be determined mathematically? In a grid, perhaps?

If so, you can store all your search terms in an array, and pretend it's two dimensional (since AS3 doesn't do real 2D arrays anyway). If you only have about 50 terms, you can do a very simple search function:

(This is sort of psuedocode, but you get the idea):


var i = 0;
while(!(search.equals(array[i])) && i <= array.length){

i++;
}

if(i<array.length){

placeInGrid = (i * rows + columns);
}

else{

//search failed
}


Then you can calculate where to put the cursor based on the position in the grid. It will look cleaner and require a lot less redundant code.

Note that there are other ways to do a linear search--probably better ones. I just wrote the first thing that came to my caffeine-deprived mind.

danhodkinson
March 15th, 2008, 03:31 PM
no it is a map so the user could search for anything, all the buildings have completely different names, so dont think that method could be used, thank you for your input though its always nice to see different methods

danhodkinson
March 15th, 2008, 04:02 PM
with my search string at the moment i am using:

function pointerAction(){
var search:String;
search = textBox.text.toLowerCase(); //converts whatever text to lower case

switch (search)
{
case "banks":
case "barclays":
case "natwest":
cursorGoto(409,108); break

case "becket court":
case "becket":
cursorGoto(447,172); break


and so on....

however as you can see i have to include searches for becket and becket court, which is likely to happen a lot, is it possible to set it so that becket court is the search term but it will match it with just becket as well?
thanks
Dan

danhodkinson
March 17th, 2008, 02:16 PM
anybody that can help me with this one?
dan

Charleh
March 18th, 2008, 04:59 AM
Yes - you could use the string functions to explode the string into it's components using a space as a delimiter, then search on each part of the array as well as the whole string

Look up string functions in AS you will find some useful ones - I think they have the split() function in AS but I've not really used it, but I'm pretty sure it's there

Anyway it would go something like




searchString = "Some words you want to seach on which have spaces";
searchArray = split(searchString, " "); // Assuming that's how the split function works

for (each str in searchArray) {
FindMatches(str);
}


Something like dat

danhodkinson
March 18th, 2008, 07:27 AM
thanks for that Charleh, but thats not really what im after as the search term in the switch is "becket court"
so if i search for becket, the split doesnt get rid of anything as there is no space, i need it to see that if yu search for becket, it matches with "becket court" and for all of my other examples.
thanks
Dan

Charleh
March 18th, 2008, 08:11 AM
You miss the point - the split is to seperate the 'becket' part from the 'court' so therefore you get a 'becket' to search with and a 'court' to search with.

Personally I'd write an associative array for each item with the list of search terms that it should match included in it - then you can write your 'match' function to return 1 item - the first closest match to the list of terms

The list of terms will start off as a string, the split will chop the string down into parts and try and match the parts of that string against any objects in the list of terms in the associative arrays, then if there is a match that object should be returned by the function - your goto should be outside of this function and just work on an object level

So the code may be like




var foundObj = FindObject(searchTerm);

if (foundObj <> null) {
cursorGoto(foundObj.x, foundObj.y);
}

function FindObject(searchterm:String) {
var searches:Array = searchterm.split(" ");
// you may want to do more complicated string processing to get every combination of words or to remove of/and/or/to/etc - short words that arent needed

for (var i = 0; i < yourarraylist.length; i++) {
for (each search in searches) {
if (term[i][0] == search) {
return term[i][1];
}
}
}
// couldnt match anything
return null;
}
// This is assuming that your array is 2 dimensional with the 1st dimension being the search term and the 2nd dimension being the object to return



If you want to do it the way you are doing it you will end up hard coding every possible search criteria - which may not be a problem if there aren't that many.

danhodkinson
March 18th, 2008, 08:31 AM
i sort of understand what you are saying,
do i write different arrays for all the different types of searches (which will be about 70)
or just 1 array with every search term?

danhodkinson
March 21st, 2008, 11:51 AM
just wondering if anybody can still give me a little bit of help with this.
i am completely lost, ive been trying for the last couple of days but seem to be going backwards