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 Professional Discussion / Passing an array name through a function....?

Author
Message
Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 3rd Oct 2010 23:00 Edited at: 3rd Oct 2010 23:01
I can't seem to figure this one out..

Basic outline.. writing a function that needs to do a linear search through an array, the name passed to the function as it is called, along with a search value.

However I get compile errors telling me the parameters are incorrect, when it hits the line that sets up my FOR/NEXT loop..

Probably doing something noobish.. But I do NOT know the name of the array in advance, nor do I know how many elements it will contain, so I cannot set a finite loop cycle.

So...what am I doing wrong?



MSon
22
Years of Service
User Offline
Joined: 13th Jul 2004
Location: Earth, (I Think).
Posted: 3rd Oct 2010 23:11
You cant pass an array name using Darkbasic Commands, maybe theres a DLL out there that will allow it, but not that I know off.

Quel
17
Years of Service
User Offline
Joined: 13th Mar 2009
Location:
Posted: 3rd Oct 2010 23:24
Put "global" before them 'dim', when you declare the array... if i got your problem correct though...
Sixty Squares
20
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 3rd Oct 2010 23:26
IanM's plugin grants you access to array pointers... Maybe that could help? Search for IanM's Matrix1 Plugin.


Guns, cinematics, stealth, items and more!
MSon
22
Years of Service
User Offline
Joined: 13th Jul 2004
Location: Earth, (I Think).
Posted: 3rd Oct 2010 23:27 Edited at: 3rd Oct 2010 23:28
if you could post some code with the arrays you need to access, i'll try and cobble something together.

Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 4th Oct 2010 00:56
This is a currently unused function, and I don't have any arrays for it dim'd. I don't call the function anywhere in the program yet, but I wanted to compile to test something else I was working on.


If that is the problem, I would have thought it would tell me the array isn't defined tho.. Because the array name is a variable, and that variable is not even assigned or the function being called - how could it tell one way or the other?

I did look at the array pointer utility, but I'm not really sure it would have much of a use for what I'm doing. Although there is also a Key:Value mapping system I was interested in using.

sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 4th Oct 2010 02:00 Edited at: 4th Oct 2010 02:01
In the function name you are defining a local variable `array_name$` as a string, then in the function referring to it as an array. Which is why the error is that the parameter types don't match.

As already said you can't pass an array to a function, so you would pass an array pointer (from IanM's commands) to the function and then manipulate the array like that. (I have never used those functions so apologies if that's not how they work!)
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 4th Oct 2010 05:29
What you do is have a predefined array then search that. For example:



By the way, arrays are global by default so it only requires a dim.

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 4th Oct 2010 12:05
I think he was wanting to use the function on a variety of arrays with different names & sizes.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 4th Oct 2010 14:49 Edited at: 4th Oct 2010 15:09
Quote: "I think he was wanting to use the function on a variety of arrays with different names & sizes."


In that case, all you need is pre-defined arrays and a sort of pointer, like having types of arrays and constants for each type. Then you just pass the type of array you want to search into the function and search that. For instance:



Or make separate functions for each array.

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 4th Oct 2010 15:07
The Plugin Way

In my 'DBP Tips and Tricks' thread, CuCuMBeR posted and example of passing array name into a function and set its properties via IanM's plugin:



Credits to CuCuMBeR for this, oh and IanM's wonderful plugin.

My thread btw: DBP Tips and Tricks

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 4th Oct 2010 18:07
Hi

Quote: "I think he was wanting to use the function on a variety of arrays with different names & sizes. "


Yes, that was what I had intended to do. Thanks for the snippets, that should give me a good idea of how to proceed.

As for defining the variable to hold the array name as a string, well a name is gonna be in string form, but also those arrays would be dimmed with an $ sign, to indicate they hold strings (I have seen this in code examples before so assumed that was what was needed).

At any rate this should put me on the right track (hopefully).

Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 4th Oct 2010 18:20 Edited at: 4th Oct 2010 18:20
If your using UDT Arrays then you can't declare them as string arrays, so:

myArray$() = works just for string
myArray$() as UDT = will end up with a type cast error

myArray() as UDT = this is what you want as long as you use it like this:

myArray(x).xxx = "sdfdsf"
and not like this:
myArray(x) = "sdfdsf"

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 5th Oct 2010 00:33
I neglected to notice my test array was in fact, declared how you mentioned it should be for a UDT Array. Funny, that..lol

Good to know though, thanks

Neco
18
Years of Service
User Offline
Joined: 13th Jul 2008
Location: Waterloo, Wisconsin USA
Posted: 7th Oct 2010 18:44 Edited at: 7th Oct 2010 18:46
Ok so this is what I have come up with. It works, and it will match the reference to an array index. Not claiming its the most concise format as I was just testing the idea. This uses Matrix 31 for the Key:Value table



However, something I haven't figured out - don't know if its possible.. is there some way to store the name of an array the same way? The idea being Keep the array name in one table, and the address in another, then reference each table and put them together (Or keep it in one table and do some string splitting to prepare it). So far I haven't been able to get that to work.

NOT a big deal, just wondering if that's possible.

Just having the table setup above, will still let me store everything in a single 1D array and reference the address I need very easily, with no searching.

And I'm assuming this would work with a UDT typed Array

Login to post a reply

Server time is: 2026-07-22 03:51:51
Your offset time is: 2026-07-22 03:51:51