Let say we have two array with same items count.
arr1 = #("a","b","c","d","e")
arr2 = #(1,2,3,4,5)
Which will be the easy way (sort code) to combine these 2 and get this output
#("a", 1, "b", 2, "c", 3, "d", 4, "e", 5)
Forum topic · General Scripting
By barigazy · 2014-03-14
Let say we have two array with same items count.
arr1 = #("a","b","c","d","e")
arr2 = #(1,2,3,4,5)
Which will be the easy way (sort code) to combine these 2 and get this output
#("a", 1, "b", 2, "c", 3, "d", 4, "e", 5)
Thanks for the discussion guys,
3dwannab · 2017-01-06
I've one but I think it's not in the order you want this just joins arrays after each other. Quite simple but it could be of use to someone passing through.
array1 = #("a","b","c","d","e")
array2 = #(1,2,3,4,5)
array3 = #("this","works","a","treat")
array4 = #("3dsMax","crashes","too","much")
fn combine_4_Arrays arr1 arr2 arr3 arr4 =
(
tempArr = copy arr1 #nomap
join tempArr arr2
join tempArr arr3
join tempArr arr4
)
newArr = combine_4_Arrays array1 array2 array3 array4
#("a", "b", "c", "d", "e", 1, 2, 3, 4, 5, "this", "works", "a", "treat", "3dsMax", "crashes", "too", "much") -- > returned array
SugaR · 2014-03-20
Hi Barigazy
I liked the idea so I tried it but I got an error. The object properties are not as string but still as integer so the dotNet function isn't working...
Here is the code I have:
fn combineArray arr1 arr2 =
(
testArray1 = #()
for i = 1 to arr1.count do join testArray1 #(arr1[i],arr2[i])
testArray1
)
fn printer propArr varArr filename: =
(
testArray2 = combineArray propArr varArr
(dotnetclass "System.IO.File").WriteAllLines filename testArray2
)
b = createInstance Box
props = #()
vals = for p in getPropNames b collect
(
append props (p as string)
getProperty b p
)
printer props vals filename:@"c:\temp\test.txt"
And here is what I get in the listener:
combineArray()
printer()
Box
#()
#(25.0, 25.0, 1, 25.0, 1, false, 1, false)
-- Error occurred in printer(); filename: D:\Travail\Projets\3D Studio Max\Script\Sugarz Tools\scripts 2014\Test Barigazy\combine_two_arrays_02.ms; position: 286; line: 12
-- Frame:
-- filename: "c:\temp\test.txt"
-- varArr: #(25.0, 25.0, 1, 25.0, 1, false, 1, false)
-- propArr: #("height", "length", "lengthsegs", "width", "widthsegs", "mapcoords", "heightsegs", "realWorldMapSize")
-- testArray2: #("height", 25.0, "length", 25.0, "lengthsegs", 1, "width", 25.0, "widthsegs", 1, "mapcoords", false, "heightsegs", 1, "realWorldMapSize", false)
-- Unable to convert: 25.0 to type: String
By the way, my pseudo isn't SugaRi, just SugaR (it wasn't free to use for registering and I didn't changed it afterwards :)
barigazy · 2014-03-20
You might wonder what purpose has this function.
Well.. for example we can easely print some object props to external file.
fn printer propArr varArr filename: =
(
array = combineArray propArr varArr
(dotnetclass "System.IO.File").WriteAllLines filename array
)
Example:
b = createInstance Box
props = #()
vals = for p in getPropNames b collect
(
append props (p as string)
(getProperty b p) as string
)
printer props vals filename:@"c:\temp\test.txt"
#3 solution by SugaRi
barigazy · 2014-03-19
fn combineArray arr1 arr2 =
(
array = deepcopy arr1
for i = 1 to arr1.count do insertItem arr2[i] array (i*2)
array
)
combineArray arr1 arr2
#1 solution
barigazy · 2014-03-14
This solution represents longest and slowest version.
fn combineArray arr1 arr2 =
(
int = 2 ; e = 1
number = arr1.count*2
do
(
insertItem arr2[e] arr1 (int)
int+=2 ; e+=1
)
while int <= number
arr1
)
combineArray arr1 arr2
#2 solution
barigazy · 2014-03-14
Easy way
fn combineArray arr1 arr2 =
(
array = #()
for i = 1 to arr1.count do join array #(arr1[i],arr2[i])
array
)
combineArray arr1 arr2
barigazy · 2014-03-14
Does anyone have 3rd solution?
SugaR · 2014-03-19
Not sure if it's a good or fast solution but it works:
testArray = arr1
for i = 1 to arr1.count do
(
j = i*2
insertItem arr2[i] testArray j
)
testArray
barigazy · 2014-03-19
This solution can works but first thing first:
If you want to prevent changes of arr1 you need to use "deepcopy" fn ei.
testArray = deepcopy arr1
Not need to use "j" variable here just
insertItem arr2[i] testArray (i*2)
"insertItem" fn is slow for big arrays.
SugaR · 2014-03-20
Ok good thing to know about the copy/deepcopy
About the "j" variable, It's just easier for me to work with variable like this when I write some code, like that it's easier to modify the expression afterwards if needed, since I have a better visibility of the expression in the code :)
Then when the code is working, I try to get rid off all this kind of useless variable to optimize the code.
barigazy · 2014-03-20
I fixed previous code. I just forgot to convert value to string
(getProperty b p ) as string
SugaR · 2014-03-20
That's nice :)
barigazy · 2014-03-20
If you have another idea for usage or any array combo post here and we can continue disscusion.
Cheers!
SugaR · 2014-03-21
I don't have any for the moment, but come back when I will :)
Otherwise, I'd liked to get a bit of improvement of what you've done here:
for now, you print each element of the combined array into separate lines in the txt file, how would you do to print all of the array elements in only one line,with some punctuations, units and the object name at the begining.
For summarize a bit, something like that:
Box01: height: 25cm, length: 25cm, etc...