How to merge two arrays in Bash Stack Overflow?

How to merge two arrays in Bash Stack Overflow?

I have the following situation, two arrays, let’s call them A ( 0 1 ) and B ( 1 2 ), i need to combine them in a new array C ( 0:1 0:2 1:1 1:2 ), the latest bit i’ve come up with is this loop: But it doesn’t work that well, as the output i get this:

Can you create a two dimensional array in Bash?

Bash does not support multi-dimensional arrays, but there is a way to imitate this functionality, if you absolutely have to. As a quick example, here’s a data table representing a two-dimensional array. And here’s the graphical representation of this two-dimensional array with the values you would expect for each y [x] position:

Can a multidimensional array take two elements?

And so it no longer will take it as 2 elements of an array as you are not keeping any info about the array data structure in your value. So, I found this hack to be sufficient with only 1 limitation that you will have to do this each time you want to do store/retrieve values.

Is there way to fake multidimensionality in Bash?

You can’t do what you’re trying to do: bash arrays are one-dimensional You can fake multidimensionality by composing a suitable array index string: I understand what you need. I also wanted the same for weeks. I was confused whether to use Python or Bash.

How to iterate over two arrays simultaneously in Bash?

From anishsane’s answer and the comments therein we now know what you want. Here’s the same thing in a bashier style, using a for loop. See the Looping Constructs section in the reference manual. I’m also using printf instead of echo. Another possibility would be to use an associative array:

Which is the intersection of A and B?

Thus, at first we have got intersection of A and B (which is simply the set of duplicates between them), say it is A/\\B, and then we used operation of inverting intersection of B and A/\\B (which is simply only unique elements), so we get B\\A = ! (B /\\ (A/\\B)).

How to get all elements in both arrays?

You can get all elements that are in both A and B by looping through both arrays and comparing: There is rather elegant and efficient approach to do that, using uniq — but, we will need to eliminate duplicates from each array, leaving only unique items.