#149920 - 2005-10-13 10:45 PM
Re: Knowledge request
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
Multidimensional arrays, the simplest multi-dim arrays are.
a 5x5 grid
Code:
0 1 2 3 4 5 a 3 s 5 y h d b e 4 5 6 7 9 c 3 e w 8 9 0 d 2 e e 4 5 6 e 2 3 1 9 9 3 f 3 4 d 7 q s
Take the values 3,C and you get 8 this is a 2 dimensional array, and the most common used.
You can represent this type of an array two ways inside of KiXtart.
Code:
;example 1 dim $array[5,5] ? $array[2,3]
or
Code:
;example 2 DIM $array[5] for $i = 0 to ubound($array) $array[$i] = $array next ? $array[2][3]
both ways have their advantages / disadvantages.
The major disadvantage of example one, is that kix can only redim the last multi-dim value. But in example 2 you do not have that limitation.
Example 2 on the other hand take a good deal bit more code to build the array, but i feel it is a good trade off for a more flexible setup.
|
Top
|
|
|
|
#149921 - 2005-10-13 10:51 PM
Re: Knowledge request
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
and example using a multidimensional array.
you have a text file with the following data in it. Code:
3 s 5 y h d e 4 5 6 7 9 3 e w 8 9 0 2 e e 4 5 6 2 3 1 9 9 3 3 4 d 7 q s
Code:
$data = loadfile("data.txt") for $i = 0 to ubound($data) $data[$i] = split($data[$i]) next
? $data[2][3]
;this Function will load the contents of a text file into an array Function loadfile($file) DIM $fso,$f,$fs $fso = CreateObject("Scripting.FileSystemObject") $f = $fso.GetFile($file) If @ERROR Exit(2) EndIf $fs = $f.OpenAsTextStream(1) $loadfile = Split($fs.Read($f.size),@CRLF) Exit(@ERROR) EndFunction
when you print 2,3 you should get "8"
|
Top
|
|
|
|
#149922 - 2005-10-13 10:59 PM
Re: Knowledge request
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
Quote:
Second is seeding of a RND, when would be the appropriate time to use them, and why would you seed a RND.
Thanks.
you will always want to seed a RND, because you see... RND is not realt all that random.
run the following code.
Code:
for $i = 1 to 10 ? RND(10) next
you get this....? Code:
G:\kix\sandbox>kix32 test
0 6 2 8 6 5 3 9 9 8
and... you get it everytime that you will run it too.
now, run this.
Code:
SRND(@msecs) for $i = 1 to 10 ? RND(10) next
that should shake things up enough to get... "random numbers". but if you always use the same seed, you will always get the same random numbers.
Edited by Bryce (2005-10-13 11:00 PM)
|
Top
|
|
|
|
#149924 - 2005-10-14 10:10 AM
Re: Knowledge request
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
and why would you seed a RND
You need to seed it because it is a pseudo-random number generator, not a random number generator.
The RNG (random number generator) works by taking a start value, pushing it through a complex calculation and ending up with another number. The problem is that first number. There is absolutely no random event that your PC can get to to establish that first number.
This means that it is up to you to find a random-ish number.
The other reason for having a seed is that a predictable stream of "random" number might be desirable. You might want to get the same numbers every time you run the program.
I use this for password generation. The BIOS on our PCs is locked down with a password. Unfortunately somtimes we need to divulge this password to employees or third party engineers so we cannot use a common password.
We could allocate random passwords and record them on a spreadsheet, but that would be an administrative nightmare.
Instead we generate random passwords using a hash of the PC serial number as a seed. This means we can always regenerate the password by typing the PC serial number into the password program.
The local administrator password is managed in the same way.
Quote:
Re: RND
so if I am using RND to generate a number in an UDF, will it generate the same RND each time I call the UDF, or will it generate the same RND each time I run the script?
The RNG state is in the KiXtart session scope. This means that you only need to seed the RNG once per kix32.exe execution.
Even if you run a number of scripts e.g. "kix32.exe script1.kix script2.kix script3.kix" you will only need to seed the RNG in script1.kix
The RNG is pretty basic, and one of it's flaws is that the first number returned is not very random at all. It is good practice to discard the first number after seeding the RNG.
Also, don't seed the RNG in the UDF as that will restart the pseudo-RNG sequence making it even less random!
Finally, don't used @MSECS alone to seed the RNG. Choose something else. @MSECS has a range of 0-999 which means that your "random" numbers will only be from a selection of a thousand possible results. We are moving from random numbers towards predictable!
@TICKS is a better option for a simple seed. A reasonable (if not perfect) simple seed might be: Code:
SRND(@TICKS*1000+@MSECS)
|
Top
|
|
|
|
#149927 - 2005-10-27 07:35 PM
Re: Knowledge request
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
A simple way to picture a multi-dimensional array might be: $X[0,0,0] = Row, Col, Page $X[0,0,0,0] = Row, Col, Page, Book $X[0,0,0,0,0] = Row, Col, Page, Book, Shelf
and so on..
Since you are desining the code, the row/col association can be anything you like - whatever makes sense to you. $X[0,0,0,0,0] = Shelf, Book, Page, Col, Row is NOT the SAME as above, but is equivelent, as long as you maintain the same references throughout your program.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#149929 - 2005-10-27 08:17 PM
Re: Knowledge request
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
Um, not exactly..
The ReDim function changes the array size declaration, optionally preserving the existing data. I don't think it allows you to add a dimension - it changes the size of an array. If your array already has 2 or more dimensions, you've reached the Twilight Zone and can only change the size of the last dimension. Arrays of Arrays, as was noted, are much more flexible in terms of resizing, but require a bit more skill and coding to manipulate.
So, if your array was 10 rows of 3 columns, you could add columns (the last dimension) but not rows. If you structured it as 3 columns with 10 rows, you could add rows, but not columns.
Very often, we create the array with the maximum number of cells we thnik we will need, and then use ReDim to resize it down to the actual number of required cells. For example, if I DIM $X[100] and then populate only 37 elements, if I later scan the array, I have to reference each element (using another counter variable) and constantly test for valid data. However, if I know that I only added 37 elements (0-36, remember - it's 0-based), I can resize my array - (assuming the counter var $Ctr post-incremented and is at 37)
ReDim Preserve $X[$Ctr - 1]
Then I could us a For Each loop to enumerate the elements of the array:
For Each $Element in $X ; note the array is $X, not $X[] $Element ? Next
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#149931 - 2005-10-27 10:05 PM
Re: Knowledge request
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
No - wrong on 2 counts.. once you define an array with a spcific number of dimensions, you can't add or remove dimensions, you can only change the size of the array. So, your ReDim could increase or decrease the number of ELEMENTS (cells) in the array, but not the number of dimensions. A one-dimensional array is a collection of rows with 1 column. You can only change the number of rows, not columns.
Secondly, [x][y] isn't a two-dimensional array, it's an array of arrays. Think of this:
$X = "a", "b", "c" This creates and populates an array, with element 0 containing "a" and element 2 containing "c"
So
Dim $X[3] $X[0] = "a", "b", "c" $X[1] = "d", "e", "f" $X[2] = "g", "h", "i"
Each element of the array "$X" contains yet another array, so you can specify
$V = $X[1][1] 'v=' $V ?
and $V will contain "e" - the second element of the $X array, and the second element of the array in that element. As you can see, it can get confusing.
Copy the above into a test script.. Then add:
$SubArray = $X[2] For Each $Element in $SubArray 'SubElement=' $Element ? Next
That should make it clear.
Then, you could actually re-dim $SubArray, and reassign it to your parent array.
ReDim Preserve $SubArray[3] $SubArray[3] = "z" $X[2] = $SubArray
'1: ' UBound($X[1]) ? '2: ' UBound($X[2]) ?
You can see that the sub-arrays in element 1 and 2 of the parent array have different values now.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#149933 - 2005-10-27 11:12 PM
Re: Knowledge request
|
Sealeopard
KiX Master
Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
|
You still don't seem to understand the difference between a multi-dimensinal array and an array of arrays. These are two distinct beasts. An Excel spreadsheet for exampel is a two-dimensional array, consisting of rows and columns.You'd reference it in KiXtart like this: Code:
dim $a[3,3] ? $a[2,3]
A three-dimensional array is like a cube or multiple layers of two-dimensional arrays, like the tabs in Excel, where each tab references one dimension. Thus Code:
dim $a[2,3,4] ? $a[2,2,2]
In an array of arrays, each element in an array is an array by itself, not a single value. Code:
dim $a[2], $b[2] $a[1]=$b
? $a[1][2]
$a[1] now references variable $b, which is an array. $a[1][2] references the second element in variable $b which is in $a[1].
_________________________
There are two types of vessels, submarines and targets.
|
Top
|
|
|
|
#149937 - 2005-10-28 02:36 PM
Re: Knowledge request
|
Sealeopard
KiX Master
Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
|
You're now introducing circular references of arrays. Please get your understanding of a simple array straight first.
To add a column to an array, you use REDIM.
For example, an array with three rows and three columns is defined as DIM $a[2,2]. To add another columns to it, you'd do a REDIM $a[2,3], if you want to preserve the existing array element contents you'd use PRESERVE, thus REDIM PRESERVE $a[2,3]. Please experiment with this first to fully understand it. You now have a total of three rows and four columns. Arrays beyond two dimensions work the same way. Imagine an array as a collections ofindividual data elements, e.g. wooden cubes. A variable is a single wooden cube. A row or column of wooden cubes would represent a one-dimensional array, with each cube being a single data element. Now add a second ropw of wooden cubes to the first one, you now have a two-dimensional array of wooden cubes.
Then, put another two rows of cubes onto the existing ones, you now have a structure resembling a cube by itself, e.g. two cubes long, two cubes high, and two cubes in depth.
Now comes the tricky, or easy part. Instead of considering a single cube as a data element assume that the cube itself is made of a number of red wooden cubes, e.g. a 3x3x3 cube of red wooden cubes. This is an array-of-arrays.
I'm giving up now, I have no idea how to simplify it even more, maybe start with a drawing?
You MUST understand the basics of arrays before you can start using them effectively.
_________________________
There are two types of vessels, submarines and targets.
|
Top
|
|
|
|
#149938 - 2005-10-28 02:45 PM
Re: Knowledge request
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
Is that correct? And what about the rest of my logic, as far as I can tell from what you are saying here it is correct.
You might be correct, but it's hard to tell
I think that you are over-complicating the process because you are new to arrays and how to use them.
Take a step back, and describe for us exactly what it is you are trying to do. We can suggest a solution which might include arrays. Or it might not!
For example, after understanding what you want to do we may well suggest that you would be better off writing the data directly to an INI file rather than storing it in an array.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 436 anonymous users online.
|
|
|