|
Post by codabasepi on Apr 17, 2018 16:00:08 GMT
Is there a way to assign an array element with a variable name such that both the array element and the variable name refer to the same memory location? For example, suppose I have an two-dimensional array Player(i,j) where "i" refers to the player number and "j" refers to an attribute of that player. Thus Player(3,6) refers to the location for player #3. I would like to assign the variable name Location to Player(3,6) so that whenever a change is made to Location, then that change is reflected in the value stored in Player(3,6). Some languages provide for an Equivalence statement to accomplish this. For example "Equivalence Player(3,6), Location" causes both variables to refer to the same memory address.
|
|
|
Post by tsh73 on Apr 17, 2018 17:06:39 GMT
99% - No
|
|
|
Post by B+ on Apr 17, 2018 17:09:28 GMT
No to variable because it wont change.
BUT! Could have a dedicated FUNCTION to access one element in array.
Function Player36() Player36 = Player(3, 6) End Funtion
The function return will be updated when Player(3, 6) is updated. Kind of silly!
Since JB does not have user defined types put all the player attributes in separate arrays:
nPlayers = 3 dim location$(nPlayers), lifes(nPlayers), topScore(nPlayers) 'ignore 0 players$ = "Steve Sally Sam" Steve = 1 : location$(Steve) = "20 100" : lifes(Steve) = 5 : topScore(Steve) = 0 '.... Sally = 2 : location$(Sally) = "100 20" '... Sam = 3 : location$(Sam) = "300 100" '... for i = 1 to nPlayers print word$(players$, i);" is at ";location$(i) next
|
|
|
Post by carlgundel on Apr 17, 2018 18:05:00 GMT
Is there a way to assign an array element with a variable name such that both the array element and the variable name refer to the same memory location? For example, suppose I have an two-dimensional array Player(i,j) where "i" refers to the player number and "j" refers to an attribute of that player. Thus Player(3,6) refers to the location for player #3. I would like to assign the variable name Location to Player(3,6) so that whenever a change is made to Location, then that change is reflected in the value stored in Player(3,6). Some languages provide for an Equivalence statement to accomplish this. For example "Equivalence Player(3,6), Location" causes both variables to refer to the same memory address. You can create functions to access positions in an array like so: function setPlayerName$(i, pname$)
player$(i, 0) = pname$
end function
function playerName$(i)
playerName$ = player$(i, 0)
end function
function setPlayerClass$(i, pclass$)
player$(i, 1) = pclass$
end function
function playerClass$(i)
playerClass$ = player$(i, 1)
end function
|
|
|
Post by carlgundel on Apr 17, 2018 18:07:28 GMT
Or you can use a SUB for setting value instead of a function, if you like.
sub setPlayerClass i, pclass$
player$(i, 1) = pclass$
end sub
|
|
|
Post by carlgundel on Apr 17, 2018 18:23:35 GMT
|
|
|
Post by Rod on Apr 17, 2018 20:28:31 GMT
Dim player(3,6) location=6 player=3 Print player(player,location)
I call it indexing, location would typically be global
|
|
|
Post by carlgundel on Apr 19, 2018 13:27:13 GMT
Of course what we are providing here is not equivalence. In the solutions that Rod and I suggested, the value is store in only one place. Since it is accessed either directly like so:
print player(player, location)
or like so:
print playerLocation$(player)
or if you wanted to use the keyed dictionary I pointed to in my blog, perhaps something like this:
print getValue$(player, ":location")
The dictionary is really more for managing a database of information, but it can be handy for other things.
|
|