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.

Newcomers DBPro Corner / Array Help

Author
Message
Igglepud
14
Years of Service
User Offline
Joined: 14th Aug 2010
Location:
Posted: 25th Aug 2010 00:57
I know how to make an array.

dim array$

data "blah" blah" "blah"


Question...how do I use the data? Such as array$ fourth entry, or random entry. I need something like

variable=array$ rnd(8)

I am also assuming the array$ takes data from the next data command in the program. If that is incorrect and I need to somehow asign it, let me know.

www.dustinmcmurry.com
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 25th Aug 2010 01:29 Edited at: 25th Aug 2010 01:30
The data isn't automatically read into the array, you have to do it yourself. You also need to specify how big the array is when you create it. Here's a quick example:




Guns, cinematics, stealth, items and more!
Igglepud
14
Years of Service
User Offline
Joined: 14th Aug 2010
Location:
Posted: 25th Aug 2010 05:08
OK...so make the array and set the size...make data with entries equal to array size...read command assigns data to the array + a number (array 1, array 45,678, etc.) Then I can us araay as a variable...gonna try this out

www.dustinmcmurry.com
Igglepud
14
Years of Service
User Offline
Joined: 14th Aug 2010
Location:
Posted: 25th Aug 2010 05:22
Nope...when I run this I get a blank screen.



www.dustinmcmurry.com
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 25th Aug 2010 07:01
woah woah woah woah woah, your code is a bit out of whack. Every loop you are declaring an array and reading the next six data statements! Also, your "data" declaration is incorrect, it should be

data "random","words","should","appear","when","printed"

words separated with commas. Your data statement reads as the string

random","words","should","appear","when","printed
That means, first iteration it *might* print out that string, and consecutive iterations (since you already read the first piece of data, every next read statement will return an empty string) will print out ""

it's also convention to put your data statements at the very bottom of your code. Your code should first declare the array, then read the data into the array, then change then "ink" your desired font color (all this outside of the loop) and then repeatedly print out the strings contained at random. It'd look and function like this:



Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 25th Aug 2010 07:15 Edited at: 25th Aug 2010 07:16
It is showing it but because you have it create the array and read the data within the DO/LOOP it doesn't work quite right. You also forgot the commas between each string so it reads it as one single string.

In the following code snip I removed the INK command in your code since the default text color is bright white anyway. The array only needs to be 5 since the first element in the array is zero. I added a RANDOMIZE TIMER() so random number picking is better (which only needs to be seen once). You also only need to dimensionalize the array and read the data once so they are outside of the DO/LOOP. And I moved the DATA statement to the end of the code (where it normally should be).



Edit @ Neuro Fuzzy:

Hey, you beat me on that one so I guess we're even.

Igglepud
14
Years of Service
User Offline
Joined: 14th Aug 2010
Location:
Posted: 25th Aug 2010 07:52
OK...I put the data staement where I did because I thought the program would not read it after the loop (since it reads top-down, I thought when it hit loop it would go back up.)

Didn't know about the commas, no wonder...

Randomize timer()...is that why in my other project I end up with the same "random" question three times in a row? If I throw that in it will fix it?

www.dustinmcmurry.com
Igglepud
14
Years of Service
User Offline
Joined: 14th Aug 2010
Location:
Posted: 25th Aug 2010 08:00
Awesome! I got it working (and I didn't just cut and paste, go me!) Now, let's say I have two arrays. How do I create a second data set that it reads but the other array doesn't? Create a GOTO at program start, then create the array and read commands after the GOTO, then use another GOTO to send it back into the load?

GOTO seems to be frowned upon, so I was just wondering if that was how to do it. It's probably way more simple.

www.dustinmcmurry.com
Igglepud
14
Years of Service
User Offline
Joined: 14th Aug 2010
Location:
Posted: 25th Aug 2010 08:17


Here is what I have. I added a second data line, and it picked it up. I know one array can read data from multiple lines, so my question then is how does the first array know to read the second one? Once data is read, is it considered "used" and so DBPro just knows to move to the next data statement?

www.dustinmcmurry.com
Igglepud
14
Years of Service
User Offline
Joined: 14th Aug 2010
Location:
Posted: 25th Aug 2010 08:21 Edited at: 25th Aug 2010 08:22


I'm guessing so.

www.dustinmcmurry.com
Igglepud
14
Years of Service
User Offline
Joined: 14th Aug 2010
Location:
Posted: 25th Aug 2010 08:40


Getting there...I get a wrong answer every time and a 0 appears after the question.

Must sleep...kids to teach...

www.dustinmcmurry.com
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 25th Aug 2010 11:03
Quote: "Randomize timer()...is that why in my other project I end up with the same "random" question three times in a row? If I throw that in it will fix it?"


Yeah, it guarantees that it will pick more random numbers because you use TIMER() as the seed. When you run it again the TIMER() will use a different seed. Without using RANDOMIZE it's possible to pick the same "random" numbers every time the program is ran.

Quote: "GOTO seems to be frowned upon, so I was just wondering if that was how to do it. It's probably way more simple."


Yes, lets avoid GOTO. If you absolutely have to use GOTO make it GOSUB instead because you can use RETURN to go back to the line just after the GOSUB. I'm glad you didn't have either one in your code snips.

Quote: "Once data is read, is it considered "used" and so DBPro just knows to move to the next data statement?"


Yes. Darkbasic knows which DATA statement was last read and it is possible to go back to previously read DATA statements using the RESTORE command. RESTORE by itself starts at the first DATA statement or if you use a label it'll go to the first DATA statement after the label (the same kind of labels used in GOTO and GOSUB commands).



Quote: "Getting there...I get a wrong answer every time and a 0 appears after the question."


The zero appears because you use a semicolon after the PRINT statement. Change it to a colon and the zero goes away. The wrong answer every time may be because your caps lock may have been on. Because you check if answer$="m" or answer$="f" if the user typed "M" or "F" it would be considered a wrong answer. To make sure the user types either a "m" or "f" in lower case use the LOWER$() command to convert the string to lower case. Also there's no need to check the answer$ twice because you can use ELSE like this:



The ENDIF above closes the IF/THEN condition.

Or if you prefer the IF/THEN on one line:


Here's all the code modified:


Igglepud
14
Years of Service
User Offline
Joined: 14th Aug 2010
Location:
Posted: 26th Aug 2010 02:15
OK, here's something I find odd. I got it working, but it was the semicolon after the print command messing it up. Somehow changing the semicolon to a colon fixed the whole problem, and I don't see how it affected the final answer at all.

www.dustinmcmurry.com
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 26th Aug 2010 08:43
Probably because it was trying to print x$ and not registering that you wanted x$ to equal "m" or "f". We use semicolons with PRINT to keep the next PRINT statement on that same line on the screen.



Bottom line you confused Darkbasic Pro.

Login to post a reply

Server time is: 2024-09-28 20:30:26
Your offset time is: 2024-09-28 20:30:26