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 / Using variables as array names

Author
Message
Alaror
13
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 17th Jun 2011 19:26
Is it possible to use a variable in place of an array name to look for data in that particular array. I have some redundant pieces of code that I'd like to clean up by doing this. An example would be instead of using...



...could I use...

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 17th Jun 2011 22:51
Make a 2D array.

dim myArray(10, 10)

for i = 1 to 10
bleh = myArray(5, i)
next i

"everyone forgets a semi-colon sometimes." - Phaelax
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 18th Jun 2011 01:41 Edited at: 18th Jun 2011 02:15
I think what you're after are pointers. For me, Pointers allow me to process different named arrays in one function, as opposed to having many functions that do the same thing.



So with these 2 lines of code you can point your arrays into a function, which would look like this...



Although my example might be overly useless, this technique will allow for some pretty flexible coding and is definitely applicable in game code. Personally, I'd LOVE if one of the DLL guru's out there could tidy up this work flow, but in the mean time it works.


Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 27th Jun 2011 02:47 Edited at: 3rd Jul 2011 16:44
I'm not sure if you want your variable name to reference the array of elements, or an element in an array. However, the following examples show you how you could reference an array element by its name, if you do not want to work with pointers.

Note that if you are talking about referencing seperate arrays of elements by a name; what springs to my mind is using an index function to obtain the first element of a sub-array in a single array. Everytime you add a new element to the sub-array, use insert at element function to add to that sub-array; and update the other array indexes. With some elbow grease one could make it work decently enough for loading and saving stuff.

Anyway... The methods used here are geared towards functionality rather than speed.

The first example below shows you how to create array search functions. The second example shows you how to use the Styx plugin to store array indexes by name.

This example demonstrates searching for a key and returning its value
It will store indexes and search from the first occurance of



This example uses the Styx plugin, to store the index names.



Login to post a reply

Server time is: 2024-11-27 21:41:04
Your offset time is: 2024-11-27 21:41:04