# Array of sets of tuple?

**URL:** <https://discourse.julialang.org/t/array-of-sets-of-tuple/1661>\
**Category:** New to Julia\
**Created:** [January 24, 2017, 10:50am UTC](https://discourse.julialang.org/t/array-of-sets-of-tuple/1661 "2017-01-24T10:50:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mposs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mposs/32/21087_2.png) [@mposs](https://discourse.julialang.org/u/mposs)\
**Post date:** [January 24, 2017, 10:50am UTC](https://discourse.julialang.org/t/array-of-sets-of-tuple/1661/1 "2017-01-24T10:50:06Z")

</div>

Hello

Is it possible to create an array of set of tuple/array?  
Say that my array is named M.  
I would like to be able to iterate over M[i,j] and that the iterator itself be a tuple/array.

Best,

Michael.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 24, 2017, 10:53am UTC](https://discourse.julialang.org/t/array-of-sets-of-tuple/1661/2 "2017-01-24T10:53:29Z")

</div>

Hi, welcome to Julia!

Yes, this kind of thing is not too difficult in Julia. The only problem sometimes is working out the syntax (i.e. what you should actually write).

Let’s start by making a simple example: a one-dimensional array of tuples:

```julia
M = [(1, 2), (3, 4)]

```

and iterate through it:

```julia
for x in M
  println(x)
end

```

Hopefully this should be enough to get you started. Do ask again if you have more questions.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 24, 2017, 10:58am UTC](https://discourse.julialang.org/t/array-of-sets-of-tuple/1661/3 "2017-01-24T10:58:16Z")

</div>

I am not sure I understand the question, but why not? Eg

```julia
julia> genset() = Set(tuple(rand(1:10,3)...) for _ in 1:10)

julia> [genset() for _ in 1:10]
10-element Array{Set{Tuple{Int64,Int64,Int64}},1}:
 Set(Tuple{Int64,Int64,Int64}[(1,1,5),(4,2,9),(7,4,1)])  
 Set(Tuple{Int64,Int64,Int64}[(2,2,4),(7,2,6),(4,10,9)]) 
 Set(Tuple{Int64,Int64,Int64}[(1,8,1),(2,6,5),(9,5,6)])  
 Set(Tuple{Int64,Int64,Int64}[(3,7,2),(3,9,6),(8,7,7)])  
 Set(Tuple{Int64,Int64,Int64}[(3,8,1),(9,7,1),(7,2,5)])  
 Set(Tuple{Int64,Int64,Int64}[(1,10,7),(10,4,4),(3,2,1)])
 Set(Tuple{Int64,Int64,Int64}[(10,4,1),(5,8,1),(9,4,3)]) 
 Set(Tuple{Int64,Int64,Int64}[(4,1,1),(4,9,6),(9,7,1)])  
 Set(Tuple{Int64,Int64,Int64}[(3,1,1),(5,1,9),(9,9,6)])  
 Set(Tuple{Int64,Int64,Int64}[(4,7,3),(6,1,3),(5,8,4)])  

```

is an `Array` of `Set`s of `Tuple`s.

---

<div class="post-metadata">

**Author:** ![mposs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mposs/32/21087_2.png) [@mposs](https://discourse.julialang.org/u/mposs)\
**Post date:** [January 24, 2017, 11:12am UTC](https://discourse.julialang.org/t/array-of-sets-of-tuple/1661/4 "2017-01-24T11:12:06Z")

</div>

Thank’s for your answer. So what I need is, for instance, one of the following:

```julia
M = Array{Array{Tuple{Int64,Int64},1},2}(i,j),
M = Array{Set{Tuple{Int64,Int64}},2}(i,j)

```

where i and j are the dimensions of my array. I tried to fill my array with push!, but it does not seem to work:

```julia
julia> M = Array{Set{Tuple{Int64,Int64}},2}(1,5)
1×5 Array{Set{Tuple{Int64,Int64}},2}:
 #undef #undef #undef #undef #undef
julia> push!(M[1,5],(1,2))
ERROR: UndefRefError: access to undefined reference
 in getindex(::Array{Set{Tuple{Int64,Int64}},2}, ::Int64, ::Int64) at ./array.jl:387

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 24, 2017, 11:20am UTC](https://discourse.julialang.org/t/array-of-sets-of-tuple/1661/5 "2017-01-24T11:20:04Z")

</div>

The way you initialize your array, it contains undef sets. Push then tries to access those undef and throws an error. Try:

```julia
julia> M = [Set{Tuple{Int,Int}}() for i=1:1, j=1:2] # initializes with empty sets
1×2 Array{Set{Tuple{Int64,Int64}},2}:
 Set{Tuple{Int64,Int64}}() Set{Tuple{Int64,Int64}}()

julia> push!(M[1,2],(99,99))
Set(Tuple{Int64,Int64}[(99,99)])

julia> M
1×2 Array{Set{Tuple{Int64,Int64}},2}:
 Set{Tuple{Int64,Int64}}() Set(Tuple{Int64,Int64}[(99,99)])

```
