LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

I need help. I'm pretty much a beginner, and I need help in multiple areas. However I do have a particular question.

Root / Programming Questions / [.]

Tiger2Created:
Does anyone know how to program so you can have a search box? That is, if I made a dictionary program, how can I have a search box so you can instantly find any word?

What have you tried? If you're an absolute beginner, you should try easier projects or even go through some tutorials. This type of thing requires experience in a few areas. If you break down the problem, you basically have this:
  • You need a way to store all this data (the words in the dictionary, for instance). Usually this is done with a TXT or DAT file on SmileBASIC, so you'd have to know how to work with these
  • You need to accept input in some way. If you want something fancy, you'll need to know how to work with graphics like drawing on the GRP. Otherwise a simple INPUT will do (but you still need to know how this works)
  • You need to come up with a search algorithm. There are simple ones like a linear search through the dictionary for words that contain the string you typed to complex ones like "Levenshtein Distance" which, when combined with some other checks, can perform a "fuzzy" search to find words even if they're misspelled.
All together, you'd need experience with working with files, data structures, graphics (maybe), string manipulation, and some rather interesting math and logic (for stuff like word distance). I would focus on one of these things at a time.

Thank you very much for answering. I am a beginner, but maybe not absolute. I know I to place sprites and do basic things like that including input. Do you think you could help me figure this out. I'm not completely sure how to use data files. I also kind of have a messy word storing system. I was trying to figure out how to put the words in a box and when they go past a certain point during scrolling, they go out of sight without everything going as well.

Our own search function here is almost unusable decent. Go search "data" up in the top right searchbar and you'll get stuff like: DATA, READ, and RESTORE which shows how to store and read data within a program. That's one way to store data; the other is in files. IDK if we have anything good on working with files, but if you look at the LOAD command in SmileBASIC and check the in-game manual, it should go over reading data from files into arrays. If you're unsure how to work with arrays, searching "array" here yields stuff like How do arrays work?. I've honestly never tried to load a lot of text data from a file before though, so I'm not sure what the best way would be. Also, getting the data into SmileBASIC can be a pain; if you don't have too much data it might be better to type it all manually in a DATA section in your program. As for the scrolling, I'm not sure what you mean. If you're using PRINT to put the words on the screen and you don't want it to wrap, you can use functions like LEFT$ to truncate the word so it doesn't try wrapping the text. You'll have to keep track of where you're printing on the screen and truncate accordingly.

if I used print, then once the words go off the screen, wouldn't they disappear so if you scroll back up they are not there anymore? Also, I'm still not sure how to create a search box. What commands would I use other than input so it searches for words?

Oh you want to be able to scroll back up? You'll have to keep a history for that, which means more complex programming. The basic idea is to have an array that holds every line you've printed, then every time you PRINT something you also put it in this array. When you want to scroll up, you reprint the whole screen using an offset into the history array. I can't give an example right now, sorry. If you want a fancy searchbox where it gives options as you type, that's quite a lot more complex than just accepting the whole word using INPUT then running your search function. You'll have to use INKEY to capture individual characters to build up a string, and also use an appropriate XSCREEN so the keyboard is always displayed (I think the default is fine though). After each character is typed, you run the string so far (that you built from each keypress) into your search function. Again, I can't give example code right now; I'm really sorry. As I said, this is a very complex project for someone who's a beginner. I would try other things that are similar but that you can do on your own with the knowledge you currently have. If you dive headfirst into something with this many unknowns, it quickly becomes impossible to proceed (as you can probably see with the ever-mounting pile of stuff you'll have to learn just to do this seemingly simple task). However, if you start a project where you can do over half of it without help, you can take smaller steps into the unknown and eventually come out with something.

Thank you. No examples are fine. I've tried simpler things before, always failing because I didn't know what to do next. The best thing I did (completed) was a calculator. I thought a dictionary would be good to do next since it's mostly just text and not sprites and whatnot. I don't mean an English dictionary, by the way. I mean an English to Japanese dictionary.

You can still make a dictionary thing; I would just aim lower. Instead of a realtime searchbar thing with scrollback, do something simple like
WHILE TRUE
 INPUT "Search word";WORD$
 IF WORD$=="QUIT" THEN BREAK
 MATCH$=FINDWORD$(WORD$) 'A function you make
 PRINT "The match: ";MATCH$
WEND
Don't worry about scrollback yet. The core of what you want is the search. That's much simpler to learn: you can use that DATA example to build up a DATA section with all the words you want to be able to search, then load them into some array and make a function to search this array. The actual "finding the word" part is separate: you should look at functions like INSTR to see if a string contains another string. After you get something little like this working, THEN you can start expanding.

Thanks for taking time to help me. It really does help. Also...you wouldn't know how to change the smilebasic source avatar, would you?

Yeah, if you go down to the bottom left (where you logged in) and click on your name, you'll be taken to your userhome page at https://smilebasicsource.com/userhome (you can also just click that link). You can change all your settings there.

Thanks. So, do you know alot about the data function?

DATA isn't a function; it's a keyword. It says that the comma separated list following it is program data that can be read with READ. There's examples on that DATA, READ, and RESTORE page, but the basic idea is that you're naming a section of data so you can read it later.
@MYWORDS 'The name of the section
DATA "cat","computer","cackle","claw" 'The data can span any number of lines and can be any number of items.
DATA "frog","flan","fire","frenzy" 'It's just a list of data pieces.
Then you do something like what they did on that example page: you RESTORE the data (using the @LABEL name) to tell SmileBASIC that you want to start reading data starting at @MYWORDS, then you use a loop to READ all the data into an array (or whatever you want to do with the items).

I guess I'll post this anyways? http://smilebasicsource.com/forum?ftid=1092

Right. So when I tried to print information from data like this: Data "Dogs" Data "Cats" Data "Lizards" All of them printed on top of each other. How do I specify for them to be in different places?

DATA has nothing to do with where they print. Code doesn't magically connect itself: you should think of program instructions as the most basic commands you can give. They don't do anything more than what you tell them to do, and DATA only tells the computer (or DS) "here's some data". The part that actually puts it on screen is you doing LOCATE and then PRINT. LOCATE also does no more than one thing: it just puts the cursor at a place on screen. If you give it the same place every time you call LOCATE, the cursor will always go to the same place. PRINT just prints wherever the cursor is.

Yeah, so how do I make them print in different places without dealing with each individual string separately? I'm still trying to set up a japanese/english dictionary.

You could use a FOR loop. A FOR loop is a section of code that repeats the code inside a certain number of times. The loop "counter" (the thing that keeps track of how many times the loop has executed) is available inside the loop. Let's say you want to start printing at location 10,5 and move down to location 10,8 (four items). You could do something like this:
RESTORE @WORDS
FOR I=5 TO 8 'I is the loop counter variable. Will start at 5, then go up to 8
  READ WORD$  'All this stuff up to NEXT is run for each value of I (5-8, so 4 times)
  LOCATE 10,I
  PRINT WORD$
NEXT 'The end of the loop

@WORDS
DATA "ONE","TWO","THREE","FOUR"
Edit: I suggest looking at some tutorials if you're having trouble with this stuff. Don't feel bad, but these are very basic concepts and you should learn them properly. There are a few general programming tutorials on the website here: https://smilebasicsource.com/search/Basic%20Tutorials

I tried that, but in stead, they still printed on top of eachother and doubled. Like a stack of words here and over there. Plus, I use more than one data instruction.

Did you try the code I posted? The first value in LOCATE is the distance across, and the second value is the distance down. Since I'm using I as the distance down, it moves down 1 line every time the loop repeats. The amount of data instructions doesn't matter. All DATA items underneath a label (like @WORDS in my example) are treated as one big list. These are equivalent:
DATA "ONE","TWO","THREE","FOUR"
'Is the same as:
DATA "ONE","TWO"
DATA "THREE","FOUR"
If you're talking about two labelled sections, now you're getting into a case where you need arrays.

I just tried it. And it works when it uses one data line, but not three.