Page 1 of 1 1
Topic Options
#169551 - 2006-10-18 11:39 AM redim one two dimensional array question
mima Offline
Hey THIS is FUN

Registered: 2002-01-25
Posts: 217
Loc: Jönköping, Sweden
Hi
I have this code and it works fine..
Code:
 
Dim $Array[5,1]

$Array[0,0] = "A"
$Array[1,0] = "B"
$Array[2,0] = "C"
$Array[3,0] = "D"
$Array[4,0] = "E"

$Array[0,1] = "1"
$Array[1,1] = "2"
$Array[2,1] = "3"
$Array[3,1] = "4"
$Array[4,1] = "5"

$counter = 0
While $counter < Ubound($array)
? $array[$counter,0]
? $array[$counter,1]
$counter = $counter + 1
Loop



Next step is to expand the size of the array with Redim Preserv.
If I code like this it wont work. I get ERROR : array reference out of bounds!

Code:
  

ReDim preserve $Array[6,1]


Top
#169552 - 2006-10-18 11:59 AM Re: redim one two dimensional array question
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Hey mima,

as a rule of thumb, always redimension the LAST dimemsion of a multidimensional array. All other tries are doomed to fail

_________________________



Top
#169553 - 2006-10-18 12:18 PM Re: redim one two dimensional array question
mima Offline
Hey THIS is FUN

Registered: 2002-01-25
Posts: 217
Loc: Jönköping, Sweden
I dont understand !!, cant I redim my $array[5,1] to $array[6,1] ?
Top
#169554 - 2006-10-18 01:26 PM Re: redim one two dimensional array question
mima Offline
Hey THIS is FUN

Registered: 2002-01-25
Posts: 217
Loc: Jönköping, Sweden
Ok Jochen, I understand now. Made this code and its working.
Code:
 Dim $Array[1,5] 

$Array[0,0] = "A"
$Array[0,1] = "B"
$Array[0,2] = "C"
$Array[0,3] = "D"
$Array[0,4] = "E"
$Array[0,5] = "F"


$Array[1,0] = "0"
$Array[1,1] = "1"
$Array[1,2] = "2"
$Array[1,3] = "3"
$Array[1,4] = "4"
$Array[1,5] = "5"

$counter = 0
While $counter <= Ubound($array,2)
? $array[0,$counter]
? $array[1,$counter]
$counter = $counter + 1
Loop
?

ReDim preserve $Array[1,Ubound($array,2)+1]
$Array[0,6] = "G"
$Array[1,6] = "6"

$counter = 0
While $counter <= Ubound($array,2)
? $array[0,$counter]
? $array[1,$counter]
$counter = $counter + 1
Loop


Top
#169555 - 2006-10-18 02:50 PM Re: redim one two dimensional array question
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Quote:

I dont understand !!, cant I redim my $array[5,1] to $array[6,1] ?




one of the reasons why i like to do array stuffing...

Code:
dim $array[3]

$array[0] = 0,'a'
$array[1] = 1,'a'
$array[2] = 0,'b'
$array[3] = 0,'c'

? $array[0][1]

redim preserve $array[4]
$array[4] = 2,'a'

? $array[4][0]
? $array[4][1]


Top
#169556 - 2006-10-18 03:10 PM Re: redim one two dimensional array question
mima Offline
Hey THIS is FUN

Registered: 2002-01-25
Posts: 217
Loc: Jönköping, Sweden
It is a bit confusing, this multiarray thing

What is the difference between
Dim $array[1]
$array[0] = 0,'a'
and
Dim $array[1,1]
$array[0,0] = 0
$array[0,1] = "a"

Top
#169557 - 2006-10-18 04:44 PM Re: redim one two dimensional array question
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Well, the first is a 1D array with an array 'stuffed' inside of it. You'd reference the data within like so...

$array[0][1] ? ; prints 'a'

The second is a 2D array. The 2D is referenced like so...

$array[0,1] ? ; prints 'a'

Top
#169558 - 2006-10-18 05:36 PM Re: redim one two dimensional array question
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Don't worry mima dude,

It took about two months (or was it a year) before the penny dropped with this array thing in my case (and this at a time where there were no real multidim arrays supported in kix).

Oh yeah, Bryce gave the right keyword, you will definetly reach one point in your scripting career where the real multidim array will fail for your needs, as only one dimension (the "last") is flexible in it. This is the point you will learn to love this stuffed array -, or array of array(s) technique.
_________________________



Top
#169559 - 2006-10-18 05:41 PM Re: redim one two dimensional array question
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
As Jochen says, an Array of Arrays is much easier for the brain to grab a hold of, and more flexible. In the most current ver of KFD I use an Array of Arrays of Arrays and that gets a little confusing, but man a great way of storing data.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#169560 - 2006-10-20 08:45 PM Re: redim one two dimensional array question
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Quote:

as a rule of thumb, always redimension the LAST dimemsion



This is nto a rule of thumb but a feature of REDIM as documented in the KiXtart Manual:
Quote:


If the Preserve keyword is specified, each dimension except for the rightmost one must be the same as those of the existing array.



_________________________
There are two types of vessels, submarines and targets.

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
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.041 seconds in which 0.021 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