Combine Two Arrays

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)

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
3dwannab's picture

Thanks for the discussion guys,

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's picture

Hi Barigazy I liked the idea

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's picture

...

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"

barigazy

barigazy's picture

#3 solution by SugaRi

fn combineArray arr1 arr2 =
(
	array = deepcopy arr1
	for i = 1 to arr1.count do insertItem arr2[i] array (i*2)
	array
)
combineArray arr1 arr2

barigazy

barigazy's picture

#1 solution

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 

barigazy

barigazy's picture

#2 solution

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

barigazy's picture

...

Does anyone have 3rd solution?

barigazy

SugaR's picture

Not sure if it's a good or

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's picture

...

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.

barigazy

SugaR's picture

Ok good thing to know about

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.