Page 1 of 1 1
Topic Options
#120298 - 2004-05-27 03:05 PM Use of multidimensional arrays
Scripter2004 Offline
Fresh Scripter

Registered: 2004-03-01
Posts: 31
Loc: Minneapolis, MN (USA)
As a KiXtart beginner, I'm a bit confused about the use of multidimensional arrays. Can they be used to create a sort of "database table", where each $Array[x,y] is a record?
I have a number of items, each of which has a status flag. Can I use a two-dimensional array to store that information? So $Array[0] being the name of the first item and $Array[0,0] its status flag, $Array[1] and $Array[1,1] for the second one, etc., or am I missing something here? I've been looking for good basic information on multidimensional arrays in KiXtart, but I can't find anything.

I'm using KiXtart 4.22.

Ron

Top
#120299 - 2004-05-27 03:08 PM Re: Use of multidimensional arrays
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
that would be :

1st Item : $array[0,0] and it's status flag: $array[0,1]
2nd Item : $array[1,0] and it's status flag: $array[1,1]
3rd Item : $array[2,0] and it's status flag: $array[2,1]

etc

Does this help ?

Oh yeah, declare the array as followed :

dim $array[ number of items - 1 , 1]


Edited by Jochen (2004-05-27 03:12 PM)
_________________________



Top
#120300 - 2004-05-27 03:39 PM Re: Use of multidimensional arrays
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
KiXtart arrays work pretty much the same as in any language.

Here is a couple of tips.

If you are going to need to change the size of the array, make the variable dimension the last one - you can only resize the right-hand most dimension.

If you are going to want to use the array like a mini-database (aka a "hash" or "associative" array), then it may be better to have an array-of-arrays. This allows you to use "Ascan" to search the "key" part of the array.

Here is an example, which uses the key "apple" to look up it's colour.

Code:
Dim $haFruitColours[1] ; Create primary array

; Create fruit name (key) array
$haFruitColours[0]="lemon","apple","grape"
; Create colour (data) array
$haFruitColours[1]="yellow","green or red","green or black"

"The colour of an apple is " $haFruitColours[1][Ascan($haFruitColours[0],"apple")] ?



The other advantage of using the array-of-arrays technique is that you may resize any of the elements.

Note the difference in addresses. A mutli-dimensional array is referenced as ARRAY[X,Y], wheras an array of arrays is referenced as ARRAY[X][Y].

Top
#120301 - 2004-05-27 03:45 PM Re: Use of multidimensional arrays
Scripter2004 Offline
Fresh Scripter

Registered: 2004-03-01
Posts: 31
Loc: Minneapolis, MN (USA)
Thanks for the info.
So $Array[0,1] would return something like "Item Title" (the title of the first item)? The status flag is either '1' or '0', so $Array[0,1] would return either '1' or '0'? So you could something like "If $Array[0,1] <statements> EndIf"?
What, then, would be returned with $Array[1,0]? Nothing?

Also, why do you initialize the second array item as '1' ($Array[items-1,1])? Is that because the value in my case is a binary flag? What if the multidimensional array contained, say, a book title and its author? Would that be something like $Array[15,15] for a maximum of 16 books and their associated authors?

Thx
Ron

Top
#120302 - 2004-05-27 03:52 PM Re: Use of multidimensional arrays
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
no still it would be [15,1]

hmmm ... how to explain ... you do give the number of elements with that declaration.

Let's see, 15,1 means that you 'create' 16 elements in the first dimension, each of this element can contain two values (second dimension), the Book title and it's author

say $array[5,0] contains the title of the 6th book and $array[5,1] it's author.

Hope I don't confuse it even more
_________________________



Top
#120303 - 2004-05-27 03:59 PM Re: Use of multidimensional arrays
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
No.

If we stick to two dimensions, an array is simply a table.
You can think of it like an Excel spreadsheet. The only wrinkle is that the numbering starts at zero not one, hence the "-1".

In your example you will have two columns, the first being title and the second author.

You will have 16 rows, one for each book.

The general form becomes:
Code:
Dim $aBooks[COLUMNS-1,ROWS-1]


Which substituting your values for 16 books is:
Code:
Dim $aBooks[1,15]



If you also wanted to record the ISBN you would have three columns in your table:
Code:
Dim $aBooks[2,15]

$aBooks[0,0]="KiXtart for beginners" ; First book title
$aBooks[1,0]="The KiXtart fanclub" ; First book author
$aBooks[2,0]="023 09120912 0129" ; First book ISBN



[edit]
Note Jochen is ordering his array [ROW,COLUMN] whereas I am ordering it [COLUMN,ROW]. Either will do just fine, but if you want to change the maximum number of rows (books) in the array you must order it [COLUMN,ROW] as you can only change the last value.
[/edit]


Edited by Richard H. (2004-05-27 04:05 PM)

Top
#120304 - 2004-05-27 04:14 PM Re: Use of multidimensional arrays
Scripter2004 Offline
Fresh Scripter

Registered: 2004-03-01
Posts: 31
Loc: Minneapolis, MN (USA)
Got it! Thanks for the info.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.061 seconds in which 0.029 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org