# How to store array data?

## Recommended Posts

How can i store multi dimensional(??) array data? for example, say i am creating 5 objects at at random positions (X,Y), how can i store that into a table of sorts to then have something checked against it?

so if i wanted to check if one object was next to another one, how would i do it?

to clarify, say i have made these. how do i store this information?:

object 1, 57x32

object 2, 35x49

object 3, 22x75

how would i subsequently check to see if say (object 4, X,Y) is at the same location as one of these objects?

##### Share on other sites

You could store these in a single dimensional array where:

• the index identifies which object you're talking about;
• the value is a string in the format"<X_Coord>|<Y_Coord>" .. which you can interpret when if necessary with StringSplit.
;One way of peeling the potato..

;populate the array with objects you mentioned ..
$asObject = StringSplit("57|32~35|49~22|75", "~") ;add another object ..$nX = 12
$nY = 120$nNewID = $asObject[0]+1 Redim$asObject[$nNewID]$asObject[0] = $nNewID$asObject[$nNewID] ="12|120";$nX & "|" & $nY ;Check if a certain object exists ..$sTarget = $nX & "|" &$nY
$ynFound = 0 For$nID = 1 to $asObject[0] If$asObject[$nID] =$sTarget Then
$ynFound = 1 ExitLoop Endif Next If$ynFound Then
;Target is found at $aObject[$nID]
Endif
##### Share on other sites

You could store these in a single dimensional array where:

• the index identifies which object you're talking about;

• the value is a string in the format"<X_Coord>|<Y_Coord>" .. which you can interpret when if necessary with StringSplit.
;One way of peeling the potato..

;populate the array with objects you mentioned ..
$asObject = StringSplit("57|32~35|49~22|75", "~") ;add another object ..$nX = 12
$nY = 120$nNewID = $asObject[0]+1 Redim$asObject[$nNewID]$asObject[0] = $nNewID$asObject[$nNewID] ="12|120";$nX & "|" & $nY ;Check if a certain object exists ..$sTarget = $nX & "|" &$nY
$ynFound = 0 For$nID = 1 to $asObject[0] If$asObject[$nID] =$sTarget Then
$ynFound = 1 ExitLoop Endif Next If$ynFound Then
;Target is found at $aObject[$nID]
Endif

<{POST_SNAPBACK}>

thanks, but, to tell the truth, i'm just more confused than ever now.

is there any way to do this without splitting strings?

if it helps, the object i am creating is a picture(GUICtrlCreatePic), and so the coordinates are not stored as a single string, but as 2 separate variables. can this be put into an array, somehow? (i still don't really understand what arrays can do, so i apologise for my ignorance)

would it help if i posted the code i'm working on?

##### Share on other sites

i think i've worked out how to store the data:

global $var[2][21] global$bob[21]

for $prog = 1 to 20$bob[$prog] = GUICtrlCreatePic("c:\dot.bmp",$left,$top,1,1) move($bob[$prog]) next (dot moved around)$var[1][$prog] =$top
$var[2][$prog] = $left (where$top is the x co-ord, $left is y co-ord and$prog is the dot number)
##### Share on other sites

[..]

is there any way to do this without splitting strings?

if it helps, the object i am creating is a picture(GUICtrlCreatePic), and so the coordinates are not stored as a single string, but as 2 separate variables. can this be put into an array, somehow?

[..]

<{POST_SNAPBACK}>

Sure - I used StringSplit simply cos it's the easiest way of populating an array with hardcoded values. .. The next easiest is to Dim a large enough array, and just assign the values. Like this:
Dim $asObject[5]$asObject [1] = "57|32" ; "x-coord|y-coord"
$asObject [2] = "35|49"$asObject [3] = $nSomeX_coord & "|" &$nSomeY_coord
$asObject [4] =$nAnotherX_coord & "|" & $nAnotherY_coord [..] (i still don't really understand what arrays can do, so i apologise for my ignorance) [..] <{POST_SNAPBACK}> Usually, arrays collect (store) values that are similar in nature. In my solution, the array is called$asObject, and it contains a series of values that represent X:Y coordinates. Since you indicated that it's important to be able to identify objects at the same coords, I concatenated them in a fashion that allows for easy comparison while still making it possible (and easy) to separate the X from the Y if/when necessary.

Another alternative would be to have two arrays: one a list of X-coords, and the other a parallel list of Y-coords. What ties them together (and makes them "parallel") is the index: so the x-coord of object 3 would be $anX[3] and the y-coord for the same object would be$anY[3] .. The problem with this approach is that checking for duplicates becomes more cumbersome

[..]

would it help if i posted the code i'm working on?

<{POST_SNAPBACK}>

Ideally you should prototype your goal in as few lines as possible in an abstract script; and then when you have the mechanism working there, apply the methodology to your actual script.

HTH

Edit: spelling

Edited by trids
##### Share on other sites

Sure - I used StringSplit simply cos it's the easiest way of populating an array with hardcoded values.    .. The next easiest is to Dim a large enough array, and just assign the values. Like this:

Dim $asObject[5]$asObject [1] = "57|32"; "x-coord|y-coord"
$asObject [2] = "35|49"$asObject [3] = $nSomeX_coord & "|" &$nSomeY_coord
$asObject [4] =$nAnotherX_coord & "|" & $nAnotherY_coord Usually, arrays collect (store) values that are similar in nature. In my solution, the array is called$asObject, and it contains a series of values that represent X:Y coordinates. Since you indicated that it's important to be able to identify objects at the same coords, I concatenated them in a fashion that allows for easy comparison while still making it possible (and easy) to separate the X from the Y if/when necessary.

Another alternative would be to have two arrays: one a list of X-coords, and the other a parallel list of Y-coords. What ties them together (and makes them "parallel") is the index: so the x-coord of object 3 would be $anX[3] and the y-coord for the same object would be$anY[3] .. The problem with this approach is that checking for duplicates becomes more cumbersome

Ideally you should prototype your goal in as few lines as possible in an abstract script; and then when you have the mechanism working there, apply the methodology to your actual script.

HTH

Edit: spelling

<{POST_SNAPBACK}>

thanks very much i didn't use your method exactly, but you helped me understand what i needed to do. thankyou.
##### Share on other sites

You're welcome

BTW .. your 2-D approach is a tidied up version of a parallel array.

TIP - You might also find it useful in such situations to use global constants to distinguish between the x-element and the y-element: It makes it easier to code (cos you don't have to remember that "the X-coord was in index=0 .. or was it 1 .. hmm ), and improves readability, which helps maintenance later. Here's an example:

;Plain code
if $aObject[$nID][0] = $aObject[$nID+1][0] Then
if $aObject[$nID][1] = $aObject[$nID+1][1] Then
;found a match
;found a match
Endif
Endif

;Self-documenting code
Global $gnX = 0 Global$gnY = 1
if $aObject[$nID][$gnX] =$aObject[$nID+1][$gnX] Then
if $aObject[$nID][$gnY] =$aObject[$nID+1][$gnY] Then
;found a match
;found a match
Endif
Endif
##### Share on other sites

You're welcome

BTW .. your 2-D approach is a tidied up version of a parallel array.

TIP - You might also find it useful in such situations to use global constants to distinguish between the x-element and the y-element: It makes it easier to code (cos you don't have to remember that "the X-coord was in index=0 .. or was it 1 .. hmm  ), and improves readability, which helps maintenance later. Here's an example:

;Plain code
if $aObject[$nID][0] = $aObject[$nID+1][0] Then
if $aObject[$nID][1] = $aObject[$nID+1][1] Then
;found a match
;found a match
Endif
Endif

;Self-documenting code
Global $gnX = 0 Global$gnY = 1
if $aObject[$nID][$gnX] =$aObject[$nID+1][$gnX] Then
if $aObject[$nID][$gnY] =$aObject[$nID+1][$gnY] Then
;found a match
;found a match
Endif
Endif

<{POST_SNAPBACK}>

Global CONST \$variable = 0

something dimmed with the CONST tag can't be changed by anything later on. Keep that in mind. The const statment basically cements the value in place...forever and ever, amen....or at least until the program terminates.

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...