Array of Array's
Moderators: Moderators for English X Forum, Scripting / Modding Moderators
Array of Array's
Um, the MSCI is a little sparse on array details. Is it possible to make an array of arrays? Is there antying specific I need to be aware while using one?
Its not a hug so much as "Help me I'm dieing!"
Hi,
there is a way to use 2 dimesional arrays
Line 7 returns ware
Line 8 returns 100
you could also use the other way
for example
$wanted.ware[0][0]=another ware
but the arrays have to be defined bevor you could use them
there is a way to use 2 dimesional arrays
Code: Select all
001 $tmp.array = array alloc: size=0
002 append ware to array $tmp.array
003 append 100 to array $tmp.array
004 $wanted.ware = array alloc: size=0
005 append $tmp.array to array $wanted.ware
006
007 $tmp.a = $wanted.ware[0][0]
008 $tmp.b = $wanted.ware[0][1]
Line 8 returns 100
you could also use the other way
for example
$wanted.ware[0][0]=another ware
but the arrays have to be defined bevor you could use them
***modified*** und stolz drauf
Why does it hurt when my heart misses the beat? Propaganda - Dr. Mabuse (1984)
Why does it hurt when my heart misses the beat? Propaganda - Dr. Mabuse (1984)
thing I often do is the following;
make an array of specific sized arrays, like an array containing nothing but other arrays of the same size, makes grouping information very easy and generally easy to find back.
example would be MDS (MD Suppliers)
it has suppliers that are stored in a general plugin array wich has an array that contains the plugin info and iirc 2 arrays
the 1st array contains arrays with the info about the suppliers their hombase and homesector, the second has a list of fighters equipped with MDs
basicly meaning I have all info from the plugin contained in 1 main plugin ... (meaning I only need to create 1 "tag" (global/local var))
G
make an array of specific sized arrays, like an array containing nothing but other arrays of the same size, makes grouping information very easy and generally easy to find back.
example would be MDS (MD Suppliers)
it has suppliers that are stored in a general plugin array wich has an array that contains the plugin info and iirc 2 arrays
the 1st array contains arrays with the info about the suppliers their hombase and homesector, the second has a list of fighters equipped with MDs
basicly meaning I have all info from the plugin contained in 1 main plugin ... (meaning I only need to create 1 "tag" (global/local var))
G
I was toying with arrays and it's somewhat tricky to understand in X3. There are no "clean" two dimensional arrays.
(I'm not a number! I AM A FREE ARRAY!)
Returns: Result = 77 77
Returns: Result = 17 77
Arrays like the subarray in the first example are like "named" entities - or objects.
Allocating a NEW array like in 2nd example line 005 forgot the old pointer to the still existing (and stored away) $Subarr and created a different subarray with it's pointer put into $Subarr again.
A small but decisive difference.
A construct like that can be passed as a data type argument or as a global variable since it is technically only a one dimensional array containing single elements of "stuff".
That the stuff is a collection of unique and distinct other arrays is an entirely different matter.
ACCESSING this construct with $Test2 = $MainArr[7][2] is an interesting shortcut since it accesses 2 entirely different objects.
This test result was so pretty I decided to post it. =)
Just like everyone else I can only guess at the proper cleanup procedure.
Assuming there is no garbage collection, one would have to iterate through all subarrays and resize them to zero.
Then resize the main array to zero.
Since there is no remove array command I can only guess that some minor residue from the header will remain.
is probably wishful thinking since that only assigns Null to the variable that stored the pointer instead of doing anything to the array itself.
Given that, it might be wise to not casually clean out subarrays to create new ones at leisure but reuse the existing ones.
Depending on the script there could be a lot of these subarrays being created and discarded. Eventually they would bloat the savegame to epic proportions.
What I wanted to use the array for won't cause trouble like that becasue creation of a subarray would only be triggered rarely and by direct player action but if it was an automatic function I would probably test it for uncontrolled savegame growth.
(I'm not a number! I AM A FREE ARRAY!)
Code: Select all
001 $MainArr = array alloc: size=9
002 $SubArr = array alloc: size=5
003
004 $MainArr[1] = $SubArr
005 $MainArr[7] = $SubArr
006
007 $MainArr[1][2] = 17
008 $MainArr[7][2] = 77
009
010 $Test1 = $MainArr[1][2]
011 $Test2 = $MainArr[1][2]
012 $test = 'result = ' + $Test1 + ' ' + $Test2
013 send incoming message $test to player: display it=[TRUE]
Code: Select all
001 $MainArr = array alloc: size=9
002
003 $SubArr = array alloc: size=5
004 $MainArr[1] = $SubArr
005 $SubArr = array alloc: size=5
006 $MainArr[7] = $SubArr
007
008 $MainArr[1][2] = 17
009 $MainArr[7][2] = 77
010 $Test1 = $MainArr[1][2]
011 $Test2 = $MainArr[7][2]
012
013 $test = 'result = ' + $Test1 + ' ' + $Test2
014 send incoming message $test to player: display it=[TRUE]
Arrays like the subarray in the first example are like "named" entities - or objects.
Allocating a NEW array like in 2nd example line 005 forgot the old pointer to the still existing (and stored away) $Subarr and created a different subarray with it's pointer put into $Subarr again.
A small but decisive difference.
A construct like that can be passed as a data type argument or as a global variable since it is technically only a one dimensional array containing single elements of "stuff".
That the stuff is a collection of unique and distinct other arrays is an entirely different matter.
ACCESSING this construct with $Test2 = $MainArr[7][2] is an interesting shortcut since it accesses 2 entirely different objects.
This test result was so pretty I decided to post it. =)
Just like everyone else I can only guess at the proper cleanup procedure.
Assuming there is no garbage collection, one would have to iterate through all subarrays and resize them to zero.
Then resize the main array to zero.
Since there is no remove array command I can only guess that some minor residue from the header will remain.
Code: Select all
$Subarr = Null
Given that, it might be wise to not casually clean out subarrays to create new ones at leisure but reuse the existing ones.
Depending on the script there could be a lot of these subarrays being created and discarded. Eventually they would bloat the savegame to epic proportions.
What I wanted to use the array for won't cause trouble like that becasue creation of a subarray would only be triggered rarely and by direct player action but if it was an automatic function I would probably test it for uncontrolled savegame growth.
My complete script download page. . . . . . I AM THE LAW!
There is no sense crying over every mistake. You just keep on trying till you run out of cake.
There is no sense crying over every mistake. You just keep on trying till you run out of cake.
Another thing I did wrong at first:
Once you SET a global array, you never need to set it again.
This is not a database entry where you have to update, unlock or whatever.
A script only needs to GET the pointer to the global array once.
Then it can freely edit any values and the changes take effect immediately.
2 scripts accessing it "simultaneously" would access the exact same object so a basic lock/unlock procedure for "records" is required.
Once you SET a global array, you never need to set it again.
This is not a database entry where you have to update, unlock or whatever.
A script only needs to GET the pointer to the global array once.
Then it can freely edit any values and the changes take effect immediately.
2 scripts accessing it "simultaneously" would access the exact same object so a basic lock/unlock procedure for "records" is required.
My complete script download page. . . . . . I AM THE LAW!
There is no sense crying over every mistake. You just keep on trying till you run out of cake.
There is no sense crying over every mistake. You just keep on trying till you run out of cake.
I'm still trying to work out how array's work. Based on what I've seen already on this topic. I should think this would work. And give me the expected output of "Array Dump = A B C D || A B C D || A B C D || A B C D ||"
Instead I get "Array Dump = nullnullnullnull |||||||||" I'm missing something.
The If index, is just part of a switch. I pass a variable to the script to determine what chunk of the code I want to execute for testing.
Instead I get "Array Dump = nullnullnullnull |||||||||" I'm missing something.
The If index, is just part of a switch. I pass a variable to the script to determine what chunk of the code I want to execute for testing.
Code: Select all
if $Index == 8
049
050 $ArrayColumn = array alloc: size=4
051 $ArrayRows = array alloc: size=4
052 $ArrayColumn[0] = $ArrayRows
053 $ArrayRows = array alloc: size=4
054 $ArrayColumn[1] = $ArrayRows
055 $ArrayRows = array alloc: size=4
056 $ArrayColumn[2] = $ArrayRows
057 $ArrayRows = array alloc: size=4
058 $ArrayColumn[3] = $ArrayRows
059
060 $Loop = 0
061
062 while $Loop < 4
063 $ArrayColumn[$Loop][0] = 'A '
064 $ArrayColumn[$Loop][1] = 'B '
065 $ArrayColumn[$Loop][2] = 'C '
066 $ArrayColumn[$Loop][3] = 'D '
067 inc $Loop =
068 end
069
070 $Loop = 0
071 $Loop2 = 0
072 $Log = 'Array Dump = '
073 while $Loop < 4
074 $ArrayBuffer = $ArrayColumn[$Loop][$Loop2]
075 while $Loop2 < 4
076 $ArrayBuffer = $ArrayColumn[$Loop][$Loop2]
077 $Log = $Log + $ArrayBuffer
078 inc $Loop2 =
079 end
080 $Log = $Log + '||'
081 inc $Loop =
082 end
083 send incoming message $Log to player: display it=[TRUE]
084 end
Its not a hug so much as "Help me I'm dieing!"
Until such time as someone explains to me what I'm doing wrong. (everything!? ) I'm going to move forward with a workaround. I'll simply create a one dimensional array with what would have been my sub array's concatenated end to end. Each entry was going to be a uniform length so I should be able to get away with multiplying my index by the intended length of the sub array.
I'm optimistic.
.... so I'll be back here in about five minutes.
I'm optimistic.
.... so I'll be back here in about five minutes.
Its not a hug so much as "Help me I'm dieing!"
there is an easier way to output the contents of an array.
if u just send the whole array to the logbook, it will displays its whole contents, upto a certain amount.
ie
should get something like
but anyways, the mistake u are making is in the displaying of the array. Your not resetting the loop count for each inner loop.
if u just send the whole array to the logbook, it will displays its whole contents, upto a certain amount.
ie
Code: Select all
write to player logbook $ArrayColumn
Code: Select all
ARRAY ( ARRAY ( 'A', 'B', 'C', 'D' ), ARRAY ( 'A', 'B', 'C', 'D' ), ARRAY ( 'A', 'B', 'C', 'D' ), ARRAY ( 'A', 'B', 'C', 'D' ) )
Code: Select all
070 $Loop = 0
071 $Log = 'Array Dump = '
072 while $Loop < 4
073 $loop2 = 0
074 while $Loop2 < 4
076 $ArrayBuffer = $ArrayColumn[$Loop][$Loop2]
077 $Log = $Log + $ArrayBuffer
078 inc $Loop2 =
079 end
080 $Log = $Log + '||'
081 inc $Loop =
082 end