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 / List Question

Author
Message
sketti188
14
Years of Service
User Offline
Joined: 7th Apr 2010
Location:
Posted: 8th Apr 2010 05:09
How can I display one random item from the first list and a completely separate item from a different/second list? For example: the first list is cities and the second is mascots. New to DB and need some help. Thanks!
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 8th Apr 2010 19:07 Edited at: 8th Apr 2010 19:08
Not sure what you mean by "lists" - do you mean arrays?

An example piece of code would be:


We use the Rnd(499) + 1 because Rnd returns a random number that is 0,1,2,...499. If we access point 0, nothing is currently set (though you could write a name into Cities$(0)). Hence we add 1 to make sure it always gives us 1 to 500.

Do the same for the mascot list, and DBC will give you another random number.

Hope this helps!

"I wish I was a spaceman, the fastest guy alive. I'd fly you round the universe, in Fireball XL5..."
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 8th Apr 2010 19:08 Edited at: 8th Apr 2010 19:09
Welcome to the forums!

To answer your question, I would suggest using arrays.

I don't have time right now, so if no one jumps in to explain arrays, I will do so later, but unfortunately I have class soon.


[edit] Oops looks like Robert beat me!

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Code eater
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location: Behind You!
Posted: 27th Apr 2010 20:01
Quote: "Not sure what you mean by "lists" - do you mean arrays?"


I don't think lists are supported by DB. I've never come across them myself in DB. However, in MS Visual C# a list is basically an array without limits. So you coululd say:



That might not make any sense to you. That is basically a list

Thanks,,,

Codeeater
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 28th Apr 2010 02:31
Is there a way to have better memory management in DBC, like adding data to an array or memblock and just expanding the memory?

Could you use a text file like a memblock and just keep adding to it?

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 28th Apr 2010 03:44
Quote: "How can I display one random item from the first list and a completely separate item from a different/second list? For example: the first list is cities and the second is mascots. New to DB and need some help. Thanks!"

List is a bit of an ambiguous term. But as others have stated above, an array is most likely what you'd need.

To tie two separate arrays together that have a relationship, you have to find something common in the relationship. This is often called an indexing key.

If you had two arrays:
Cities$()
Mascots$()

You can link them by having some common value. If we look at them like fields in a database table, we can use a third array Table() to link the two other arrays together:

Dim table(10,1)

Where the first index, 10, represents how many records +1, and the second index ,1, represents two values : 0 stores the city number, and 1 stores the mascot number

so table(0,0) holds the value of the city for the first record
table(0,1) holds the value of the mascot for the first record
And so on for the rest of the records.

Let's say the first record in the table has the city as London, and the mascot is a Cat. In our cities$() and mascots$() arrays, we would have the names of the various cities and the various mascots stored as separate indexes:

Mascots$(0)="dog"
Mascots$(1)="cat"
etc.

Cities$(0)="New York"
Cities$(1)="Xiang Hai"
Cities$(2)="London"
etc.

In the table() array, we would set up a link between cities and mascots by assigning the index numbers to the correct record in the table:

table(0,0)=2 (index 2 is London in the cities$() array)
table(0,1)=1 (index 1 is cat in the mascots$() array)

If I choose the first record from the table(), I can get the City and the Mascot that belongs to that record:

print Cities$(table(0,0))
print Mascots$(table(0,1))

So if the cities$() and mascots$() arrays were filled in, and I assigned these value to the records in the table, I could randomly choose any single record in the table and get back the City and the Mascot that belongs to that city.

This is a complex way of managing arrays but it's the beginning of setting up what could be a very versatile database. You could add additional "fields" to the table of various data types.

A simpler way to deal with everything as the same data type (strings in this case) would be to use one 2 dimensional array:

dim information$(10,1)
where 10 is the number of records +1
and 1 are the number of pieces of data stored per record +1

so we could say
information$(0,0)="London"
information$(0,1)="Cat"

This is more direct and easier than using the table linking method and will probably serve for most situations.

I mentioned the table linking method because it is a way to manage data without repeating the data. If you had 20 cities that all had cats as mascots, you could store "cat" once in the mascots array, and then just link that one item of data to each city that needs it instead of taking up memory by storing "cat" 20 times.

Enjoy your day.
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 28th Apr 2010 19:37 Edited at: 29th Apr 2010 19:35
Wow latch, I like the table idea, never thought of that one.

Alternatively, though its a very basic level of doing things, is just keep the indexes the same.

So if London had a mascot of Cat and Los Angeles had a mascot of a Spider Monkey, the code could look something like this:

That way, to pick one at random, you only have to randomize 1 variable, and just use it as the index location like so:

selectrandomplace=rnd(10)
print cities$(selectrandomplace)
print mascots$(selectrandomplace)

and it would give the appropriate data. Its no where near as robust as the table method, but I it maintains code readability (with the unique array names) and it is simple to understand. But as Latch said, it does have the problem where you will probably have to write cat 20 times.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Query Micle
User Banned
Posted: 2nd Jun 2010 08:14
great,

A faithful friend is hard to find.
<a href="http://www.buy-teragold.com" target=_blank><b>Tera Gold</b></a>

Login to post a reply

Server time is: 2024-04-26 11:03:42
Your offset time is: 2024-04-26 11:03:42