Is there any way you can give a function to an array?
I have these arrays that all have the attribute .symbol and at some point I need to search the arrays to find an element who's .symbol matches a given string.
At the minute I need one function for each array but all the functions are identical. Being a champion of efficient code this sickens me.
Check it out:
type foo
symbol as string
endtype
dim stuff(3) as foo
dim things(3) as foo
dim crapola(3) as foo
stuff(1).symbol="Imperium Galactica"
stuff(2).symbol="Missionforce:Cyberstorm"
stuff(3).symbol="Master Of Orion"
things(1).symbol="Battlezone 2"
things(2).symbol="Hostile Waters"
things(3).symbol="Mechwarrior 3"
crapola(1).symbol="Diablo 2"
crapola(2).symbol="Dungeon Siege"
crapola(3).symbol="Neverwinter Nights"
function check_stuff(s$)
element=0
for i=1 to array count(stuff(0))
if stuff(i).symbol=s$
element=i
exit
endif
next i
endfunction element
function check_things(s$)
element=0
for i=1 to array count(things(0))
if things(i).symbol=s$
element=i
exit
endif
next i
endfunction element
function check_crapola(s$)
element=0
for i=1 to array count(crapola(0))
if crapola(i).symbol=s$
element=i
exit
endif
next i
endfunction element
what I really want to do is just have one function that does the checking but I tell it what array to check. So instead of checking things(0) by calling check_things() I want to have a check_array() that looks like this:
function check_array(arrayToCheck(0), s$)
element=0
for i=1 to array count(arrayToCheck(0))
if arrayToCheck(i).symbol=s$
element=i
exit
endif
next i
endfunction element
and I would say check_array( things(0), "text" )
When I tried it it didn't like it. I want to know if such a thing is possible before I keep plugging away. Remember: I KNOW arrays are global already. I just want less repetition in my code.
Thanks!
Ending a sentence with a French word is so passé