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 demonstrates searching for a key and returning its value
` It will store indexes and search from the first occurance of
the first character of the given name
type keyType
dataType as integer
index as integer
name$
endtype
` Custom data types
#constant TYPE_STRING 0
#constant TYPE_FLOAT 1
#constant TYPE_MY_ENTITY 1
` Array to store entry names (keys)
dim keys(0) as keyType
` Array to store character indexes; to speed up searches, search from the first occurance
dim charIndex(255) as integer
` The actual data arrays
dim entities(0) as string
dim strings(0) as string
dim floats(0) as float
` Create our entries
AddEntity("First Entity")
Entities( GetIndex( "First Entity" ) ) = "Entity data"
AddString("My String","Wonderland")
AddFloat("My First Number",123)
AddFloat("My Second Number",456)
` Our indexes are pushed in, placed at the start of the character index position
` The last index is empty; so only iterate to the end - 1
print "Index list:-------------------"
for i = 0 to array count( keys() ) - 1
print keys( i ).name$; " "; keys( i ).index
next i
print "------------------------------"
` Display our results. It is best to store indexes for later use, if still valid.
` Every time GetIndex is run, a new search starts. Additional functions could be added to speed things up.
entIndex = GetIndex("First Entity")
print "The new entity has the following data: "; entities( entIndex )
print "The string is "; strings( GetIndex("My String") )
print "The number is "; floats( GetIndex("My Number") )
print "Press any key to exit"
wait key
end
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// Add an entity and create an index key //
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
function AddEntity( sName$ )
` Require a position to start adding the keys which begin with the starting character
` This will push new indexes at the index start
insertAt = setCharIndex(sName$, array count( keys() ) )
` Add our entity
add to stack entities()
` Store the reference information
array insert at element keys(), insertAt
keys(insertAt).dataType = TYPE_MY_ENTITY
keys(insertAt).index = insertAt
keys(insertAt).name$ = sName$
endfunction
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// //
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
function AddString( sName$, sString$ )
insertAt = setCharIndex(sName$, array count( keys() ) )
add to stack strings()
array insert at element keys(), insertAt
keys(insertAt).dataType = TYPE_STRING
keys(insertAt).index = insertAt
keys(insertAt).name$ = sName$
strings(insertAt) = sString$
endfunction
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// //
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
function AddFloat( sName$, fFloat# )
insertAt = setCharIndex(sName$, array count( keys() ) )
add to stack floats()
array insert at element keys(), insertAt
keys(insertAt).dataType = TYPE_FLOAT
keys(insertAt).index = array count( floats() )
keys(insertAt).name$ = sName$
floats(insertAt) = fFloat#
endfunction
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// Searches for an index. Please store the index for later use //
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
function GetIndex( sName$ )
start = charIndex( asc( left$( sName$, 1 ) ) )
for search = start to array count( keys() )
` Store the index because we cannot return from a UDT array
index = keys( search ).index
if keys( search ).name$ = sName$ then exitfunction index
next search
print "Key "; sName$; " not found. Press any key to exit" : wait key : end
endfunction -1
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// Store an index of the first occurance of a given character //
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
function setCharIndex(sName$, index)
firstCharCode = asc( left$( sName$, 1 ) )
if charIndex( firstCharCode ) > 0
index = charIndex( firstCharCode )
else
charIndex( firstCharCode ) = index
endif
endfunction index
This example uses the Styx plugin, to store the index names.
` String referenced list entries
` This Styx snippet demonstrates referencing a value through a name from a given string
` It aims to provide such functionality without much focus on processing speed
` Chris Tate - www.binarymodular.com
StyxExample:
cls rgb(0,30,5)
` This example uses the Styx plugin to store named indexes
` Note that for this example, the names are case-sensitive
` You can use the lower$ or upper$ case functions to
` compare names without case sensitive comparisons
` Declare our globals
gosub declare_styx_lists
` Declare a UDT Array
type objectType
content$
endtype
dim MyArray() as objectType
AddString("MyString","Tommy Noobican")
AddInteger("MyInteger",200)
AddFloat("MyFloat",7.1)
` We will also add a UDT entry
AddObject("MyIndex")
MyArray().content$ = "New content"
`--------------------------------------------------------------------------
` Now let us print out those values
print GetString("MyString"); " is the string saved"
print GetInteger("MyInteger"); " is the integer saved"
print GetFloat("MyFloat"); " is the float saved"
wait 3000
`--------------------------------------------------------------------------
` We can also reference a UDT array index with the following function
print MyArray( IndexOf("MyIndex") ).Content$; " is the content added to our UDT object"
wait 3000
print "Press any key to exit"
wait key
end
//////////////////////////////////////////////////////////////////////
// Add a string and return 1 if the operation succeeded //
//////////////////////////////////////////////////////////////////////
function AddString( sName$, sString$ )
` We will only add the string if it does not exist; however, this is optional
result = 1
` The Styx expression var is an expression editor that can store variables
` We will prefix each variable so that we can filter out any other expressions that contain our prefix
if expression var exist( Prefix$ + "s_" + sName$ ) = 0
array insert at bottom strings()
add expression var Prefix$ + "s_" + sName$, array count( strings() ) - 1
strings( array count( strings() ) ) = sString$
result = 1
else
result = 0
endif
endfunction result
//////////////////////////////////////////////////////////////////////
// Add an integer and return 1 if the operation succeeded //
//////////////////////////////////////////////////////////////////////
function AddInteger( sName$, iInteger )
` We will only add the string if it does not exist; however, this is optional
result = 1
` The Styx expression var is an expression editor that can store variables
` We will prefix each variable so that we can filter out any other expressions that contain our prefix
if expression var exist( Prefix$ + "i_" + sName$ ) = 0
array insert at bottom integers()
add expression var Prefix$ + "i_" + sName$, array count( integers() )
integers( array count( integers() ) ) = iInteger
result = 1
else
result = 0
endif
endfunction result
//////////////////////////////////////////////////////////////////////
// Add a float and return 1 if the operation succeeded //
//////////////////////////////////////////////////////////////////////
function AddFloat( sName$, fFloat# )
` We will only add the string if it does not exist; however, this is optional
result = 1
` The Styx expression var is an expression editor that can store variables
` We will prefix each variable so that we can filter out any other expressions that contain our prefix
if expression var exist( Prefix$ + "f_" + sName$ ) = 0
array insert at bottom floats()
add expression var Prefix$ + "f_" + sName$, array count( floats() )
floats( array count( floats() ) ) = fFloat#
result = 1
else
result = 0
endif
endfunction result
` The following functions will return the values under the key names
` Note that a default value could be added to the parameters instead of causing an error
//////////////////////////////////////////////////////////////////////
// //
//////////////////////////////////////////////////////////////////////
function GetString( sName$ )
if expression var exist( Prefix$ + "s_" + sName$ )
` Note that the expression variables are floating point numbers
return$ = strings( int( get expression var( Prefix$ + Name$ ) ) )
exitfunction return$
else
print "String array key invalid."; sName$; ". Press any key to exit" : wait key
end
endif
endfunction ""
//////////////////////////////////////////////////////////////////////
// //
//////////////////////////////////////////////////////////////////////
function GetInteger( sName$ )
if expression var exist( Prefix$ + "i_" + sName$ )
` Note that the expression variables are floating point numbers
iReturn = integers( int( get expression var( Prefix$ + Name$ ) ) )
exitfunction iReturn
else
print "Integer array key invalid."; sName$; ". Press any key to exit" : wait key
end
endif
endfunction 0
//////////////////////////////////////////////////////////////////////
// //
//////////////////////////////////////////////////////////////////////
function GetFloat( sName$ )
if expression var exist( Prefix$ + "f_" + sName$ )
` Note that the expression variables are floating point numbers
return# = floats( int( get expression var( Prefix$ + Name$ ) ) )
exitfunction return#
else
print "Float array key invalid."; sName$; ". Press any key to exit" : wait key
end
endif
endfunction 0.0
//////////////////////////////////////////////////////////////////////
// Add a new object to our array and save an index name //
//////////////////////////////////////////////////////////////////////
function AddObject( sName$ )
add to stack MyArray()
add expression var Prefix$ + sName$, array count( MyArray() ) - 1
endfunction
//////////////////////////////////////////////////////////////////////
// Get index or return an invalid index //
//////////////////////////////////////////////////////////////////////
function IndexOf( sName$ )
if expression var exist( Prefix$ + sName$ )
` Note that the expression variables are floating point numbers
iReturn = int( get expression var( Prefix$ + Name$ ) )
exitfunction iReturn
else
print "Array key invalid."; sName$; ". Press any key to exit" : wait key
end
endif
endfunction 0
//////////////////////////////////////////////////////////////////////
// //
//////////////////////////////////////////////////////////////////////
declare_styx_lists:
` We will declare 3 arrays to store 3 different datatypes
` Other datatypes and UDTs could be added
dim Strings() as string
dim Integers() as integer
dim Floats() as integer
global Prefix$ = "arr_"
return
ListExample:
MemblockExample:
` www.binarymodular.com - Help, tutorials, code generator and more goodies