- Home /
merging multidimensional arrays
hi folks!
i have a multidimensional array and i want to merge all its content to one big onedimensional array with randomized order.
is there an easy way in unity do do this?
thnx!
EDIT:
i want to merge
arr1 = ["foo 1", "bar 1"]; arr2 = ["foo 2", "bar 2"];
arr12 = [arr1, arr2] = [ ["foo 1", "bar 1"], ["foo 2", "bar 2"] ];
//searching for a method to quickly have
arrMerged = ["foo 1", "bar 1", "foo 2", "bar 2"];
You might want to add more detail to this like what language you're using. In Unity you could do this in Javascript or C#.
for now i use javascript. but both ways should be interesting to different people...
Are you looking to merge "true" multidimensional arrays, or are you using nested javascript arrays?
@headkit, remember to upvote good answers (in addition to checkmarking).
Answer by Max Kaufmann · May 04, 2010 at 02:54 PM
One options is to do a randomized Insertion-Sort (http://en.wikipedia.org/wiki/Insertion_sort). Of course, there's hackier ways, too:
// merge contents, adding a random prefix
var letters = [ "a", "b", "c", ... ];
var result = [];
for (var i=0; i<arrays.Length; i++) {
for (var j=0; j<arrays[i].Length; i++) {
result = result.Push( letters[Random.Range(0,26)] + arrays[i][j] );
}
}
// sort on the random prefixes to shuffle
result.Sort();
// removing the random prefixes
for (var i=0; i<result.Length; i++) {
result[i] = result[i].Substring(1,result[i].Length);
}