Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / need helps with searching inside an array

Author
Message
BCIT TKenobi
14
Years of Service
User Offline
Joined: 27th Jan 2010
Location: Naboo
Posted: 28th Jan 2010 16:24
heres the code


i need to search for a name within the array
how can i find it without it printing the other names and only print that one name with the coordinates and noone else
thank you
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 28th Jan 2010 18:38 Edited at: 28th Jan 2010 18:39
Don't use:

dim names$(6)
dim positionx(6)
dim positionz(6)


Use:

Dim Data$(6,2)

When you input data use



Then, when you search, you search for the required name in Data$(i,0) in a For..Next loop. If for example it was found at position where i=3 then the X and Z positions for that name would be in Data$(3,1) and Data$(3,2).

Convert them back to numbers from a string with:



[Edit] Oops - forgot to add: If you want further info on arrays, see here:

http://forum.thegamecreators.com/?m=forum_view&t=96069&b=7&p=0

TDK

BCIT TKenobi
14
Years of Service
User Offline
Joined: 27th Jan 2010
Location: Naboo
Posted: 28th Jan 2010 19:54
ok thank you so much
this is kinda confusing to me so you will probably here more from me lol
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 29th Jan 2010 07:51
I disagree TDK,
Using one array called "Data" is vague and it's easy to forget what data is stored in which field. When storing different data types it becomes even more impractical.

If you use separate arrays it is much clearer what each one is storing and cross-referencing is just as easy.


The two position arrays could be condensed into one but I don't see the need for it.

Even this is ambiguous, you might well assume that the second position value would describe the Y axis not Z.

@Kenobi
First of all, Welcome to the forum
Your program isn't very user-friendly at the moment. The user prompts "Type in the name" don't tell the user what these names are for, and the second prompt doesn't mention that it will search the array for the name typed. Make sure you give clear instructions.

Your search array function isn't producing one name because you don't have a condition in there to restrict what is printed - since you have no conditions at all in your code I will explain.

A condition is a statement that is either TRUE or FALSE, the code that is attached to the condition will only be executed if the condition is TRUE. Try running the following code.

From the output you'd think the first line never happened.
If we want some output for the first line, even if it's false, we can add an ELSE statement to the condition - code following ELSE is only executed if the condition is FALSE.

Those lines are getting pretty long. Luckily, you can write IF statements on multiple lines, this is useful when you want to do lots of things on condition.

Much easier to read. ENDIF calls the end of the statement, everything between IF and ENDIF will be executed on condition, there is no need for THEN. Notice how I've indented the lines in between? It's important to do that so you can see where the condition begins and ends. I recommend indenting any command that uses more than one line like this.

I got rid of the second ELSE because we don't need it.
Do we really need the ELSE on the first condition though? Yes it is FALSE but it will always be false, so it seems a bit silly to be wasting time writing code for something that's never going to happen. Can you work out how to write this condition without using ELSE?

That's enough on conditions, back to your code!
The first thing you did with your function is pass the search string to it - good job, we will certainly need that. At the moment however, the string isn't being used at all.
What we need to do is write a condition that will only print a name if it matches the search string.
You can compare strings in the same way as numbers (ie "a"="a" is TRUE). Can you work out the condition you should attach to the print command?

BONUS POINTS: With the right condition this program will work, but the string given will have to match the name exactly, even down to the case of each letter. If the search worked whether the letters were upper case or lower case it would be much easier to find the names you wanted. There are two commands that convert all the characters in a string to the same case: UPPER$() converts the string to upper case and LOWER$() converts to lower case. If you convert the name string and search string to the same case it wont matter which letters are capitalised.
Here's an example:


"With games, we create these elaborate worlds in our minds, and the computer is there to do the bookkeeping." - Will Wright
BCIT TKenobi
14
Years of Service
User Offline
Joined: 27th Jan 2010
Location: Naboo
Posted: 29th Jan 2010 16:24
@0Bese87
Thank you for the welcome
It will take me some time to get the basics perfect, but i believe I've figured out your question on how to write the condition without using else.


I've changed the input so it's more user friendly.


Thank you for the time you've put into helping me.
I'm just unsure of where to put the condition statement.

@TDK
Thanks for the more info on arrays. When i find time I will search more into the forums for more beginner aid.
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 30th Jan 2010 03:35
Quote: "Using one array called "Data" is vague"


It was only an example array name - it could be called anything.

Quote: "If you use separate arrays it is much clearer what each one is storing and cross-referencing is just as easy."


That's provided the data in the name array doesn't change - otherwise you have to make the same changes to all three arrays every time you do anything.

Say for example, the next task is to sort the array of names into alphabetical order?...

Mine will still have the X/Y data attached to the names afterwards.

I have to admit that doing it Obese's way is a little easier to understand, but is less future-proof.

Both will work fine with what you currently want to do.

TDK

BCIT TKenobi
14
Years of Service
User Offline
Joined: 27th Jan 2010
Location: Naboo
Posted: 30th Jan 2010 04:29
Ok, thank you TDK.

When I get back to school I will try juxtaposing the two to see which is easier for me to understand.

Until then I will search the forums for more info.

Thanks again.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 31st Jan 2010 19:09
@Kenobi
No problem
I like talking about basic stuff, it reaffirms that I have actually learned something

Quote: "I'm just unsure of where to put the condition statement.
"

Think about how YOU would perform this task. If you had a list of names and I gave you a name to search for, what would you do?
You'd look at each name in the list and see if it matches the name I asked for. Once you found the list you'd point at it and say "found it", now you'd stop looking through the list.

At the moment your function looks through the list but it's just blindly printing every name as it reads. If you put a condition on the print command you can make it only print the name that matches the search string. Once the string is a match you can print and exit the loop. You can exit any loop with the EXIT command.

@TDK
Quote: "Mine will still have the X/Y data attached to the names afterwards."

It will? How does that work?

I was mainly piping up because you made it sound like using multiple arrays was "wrong".

Question: Is searching a single character faster than a long string? If you had a huge database would it be worth checking the first character then the second and refining the search like so each time?

"With games, we create these elaborate worlds in our minds, and the computer is there to do the bookkeeping." - Will Wright
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 1st Feb 2010 03:44 Edited at: 1st Feb 2010 03:44
I would do it OBese87's way just because I don't know how fast the str$() command is. For example wouldn't it be faster to do this:



Than this?



because then whenever you wanted to change a value you would have to use the VAL() and STR$() commands to convert from strings to numbers and then back to strings. If there is no speed loss then this could actually be a pretty useful tactic though

<-- Spell based team dueling game!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st Feb 2010 22:34
Using 1 multidimensional array versus 3 parallel arrays increases data integrity, but I don't like the idea of doing string conversions when data is called. I'd probably keep the 3 separate arrays, but write helper functions that control the data inside them. As long as the data is moved or changed through the function then you have more stable control over the data when it's matched with parallel arrays.


"Any sufficiently advanced technology is indistinguishable from magic" ~ Arthur C. Clarke
BCIT TKenobi
14
Years of Service
User Offline
Joined: 27th Jan 2010
Location: Naboo
Posted: 3rd Feb 2010 14:36 Edited at: 3rd Feb 2010 14:58
I've compared the two possible ways of doing this code and I've concluded that for the time being Obese's way is easier for me to comprehend. When I am able to understand more of this I will try more possibilities.

Thank you all for chipping in. Sooner or later I will start to get this.

Here is the final code:
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 4th Feb 2010 07:00
Looks good although i do have a suggestion. At this point your function will search and if it finds the string it will print the info. Good so far. But what if it doesn't?

All variables inside function in dbc are static. this means that whatever the value of the variable was when you left the function it will be the same when you call the function again. so lets say you called your function and found the name at position 3 in the array. The next time you called it you didn't find it. Since you never reset the position variable the function will return 3 again. Understand?

Since you are starting a index 0 you could initialize position variable to -1 at the beginning of the function so that if the name wasn't found it would return -1 as a error indicator. Of course you would need to check for this error after you call the function. (using your newly acquired condition skillzzz )

New Site! Check it out \/
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 4th Feb 2010 19:03
I tend to put as much as possible in a single array.. I think that habit is the best thing, as you just remember what you did last time. XYZ is always in that order, so you know which is which, or which comes first.

Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 5th Feb 2010 04:02 Edited at: 5th Feb 2010 04:02
@Pincho
Yeah things like co-ordinates I put in the same array too, but if you had an rpg character with 9 abilities (i.e. strength, stamina, magic, gold, runes, health, etc...) I wouldn't even try to remember the order I stored them in. If you really want to use one array I would recommend making some "label" variables, this means you don't have to remember numbers and you type the name of the data you want.
For example

It would help if DB could declare constants.

@Kenobi
You are so close but it's still slightly wrong. (Do you have a computer with DB on to test your code?)

You should indent the for loop as its inside a function.
Functions have a local scope; they can only read/write variables that are declared inside the function - arrays are an exception and can be used globally (everywhere). The reason functions act like that is so you can put them in any program and they will work the same, this compatibility is what makes functions really useful.
So your function is unaware of the temp$, but since DB is a funny old language, instead of returning an error it declares temp$ as a new empty string, so the name will never match temp$.

The string passed to the function is called name$, so to fix your problem we just change temp$ to name$. However, I don't like that name because your array is also called name$(), that could get confusing. What if we called the search name search$?

Now that is fixed

Caleb made a good point "what happens if you don't find the string?", can you work out how to use -1 like he suggested?
The static nature of variables in functions is another good point.
You should always assign variables yourself at the top of the function to ensure you don't have unexpected results.

Our suggestions might be hard for you to follow but that's only because we have been coding for a while, try to break down what the code is doing into simple operations, think why we want to do it that way, what other ways are there to do the same thing, what is the difference between the methods?
There are so many ways to solve programming problems, that's why it never gets boring , if you learn how to use all the commands you can quickly figure out the best way to solve a problem.
If you ever get the feeling you aren't doing something right, or there must be an easier way of doing it, have a look in the help files or ask on here.

[edit]
Woooaah long post!

"With games, we create these elaborate worlds in our minds, and the computer is there to do the bookkeeping." - Will Wright
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 5th Feb 2010 05:38
Quote: "So your function is unaware of the temp$"


I should have read closer! lol i didn't even catch that!

New Site! Check it out \/
BCIT TKenobi
14
Years of Service
User Offline
Joined: 27th Jan 2010
Location: Naboo
Posted: 5th Feb 2010 15:34
This programming is just like space, infinite possibilities. Yeah I still have to get used to coding, but I'll get there one baby step at a time.

I will definitly come back here when I need help because you guys really know what your talking about! lol

I'll change the temp$ when I can, but I'm getting swamped with school work. lol you know how it goes.

Thank you for your cooperation in this matter.

Login to post a reply

Server time is: 2024-04-19 18:42:32
Your offset time is: 2024-04-19 18:42:32