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 AppGameKit Corner / Passing an array into a function...

Author
Message
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 21st Aug 2017 20:27 Edited at: 21st Aug 2017 21:13
The title is not quite accurate. I use a function to search arrays for a certain value. I currently have about 10 such functions, because I do not know how to pass the array through or if that is even possible.

Thanks! I would really love to have only one function instead of creating a function each time I need an array searched.



My hope:





Well... I tested it and it seemed to work...
Supertino
6
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 23rd Aug 2017 08:38 Edited at: 23rd Aug 2017 08:57
Also and this is not so useful in some ways you can use the .find() method on a sorted array;


Arrays can now be sorted using the .sort() command and searched using the .find(item) command. For example

myArray as integer[5] = [3,4,1,5,2,6]
myArray.sort() // array is now [1,2,3,4,5,6]
index = myArray.find(4)
// will return "3" as array indexes start at 0


.sort() will always sort in ascending order and .find(item) will only work on arrays that are in ascending order. If .find(item) cannot find the item you are looking for it will return "-1". You can sort arrays of Integers, Float, Strings, and Types. When sorting types the first variable of the type will be used to compare elements.
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 27th Aug 2017 16:35
I've run into another problem. Arrays as types can't be passed through as shown in the array help guide.

For instance - array.ID ref as integer[] errors out. My guess is because the array is not defined as an integer but a type. Would i need to create a new function for each type? And just use this code: myarray.ID ref as mytype[]?

Sorting is not an option.

Thanks!!
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 28th Aug 2017 03:11 Edited at: 28th Aug 2017 03:17
Hi Golelorn, yes I believe all of us go through this. We all want to pass different types into the same functions.

While this is not possible, you can always work on some tricks.

One of them is about putting similar types inside a greater type.



You can also do the same with 2 different types passing both of them as arguments inside the same function and checking which one to process using conditionals. The good thing you can assign a local array to get the type data and then do the inverse path to update the type if necessary (don't forget to use "as ref" though).

Hope it helps!
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 29th Aug 2017 02:36
I did do something similar with one of my functions,. I was hoping I misunderstood/missed something to save myself some writing,

Oh well, thank you Richard_6. It does help me. Arrays are very hard to wrap my head around, but the community has really helped.

Login to post a reply

Server time is: 2024-03-29 14:28:38
Your offset time is: 2024-03-29 14:28:38