# Array and Table not updating in a loop

**URL:** <https://discourse.julialang.org/t/array-and-table-not-updating-in-a-loop/36442>\
**Category:** General Usage\
**Tags:** array, loops, data\_structures\
**Created:** [March 24, 2020, 12:27pm UTC](https://discourse.julialang.org/t/array-and-table-not-updating-in-a-loop/36442 "2020-03-24T12:27:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Post date:** [March 24, 2020, 12:27pm UTC](https://discourse.julialang.org/t/array-and-table-not-updating-in-a-loop/36442/1 "2020-03-24T12:27:14Z")

</div>

Hello, I am getting some strange behaviour in a loop which is reading some columns from a table and converting it to an array. The loop seems to exit after the first iteration. I have given a simple example below. If I check the table `Subset` and the array `ArrayX` they show values for the when `i.Scenario == 1`,whereas I would have expected it show values of either the last iteration i.e. `i.Scenario == 2` or for all iterations.

Am I missing anything? Is there a way to update the values in the table and array?

```julia
using JuliaDB
using Tables
DataT = table([1,1,1,1,1,2,2,2], [1,2,3,4,5,1,2,3], [10,15,100,115,101,10,15,100],[210,215,1200,1215,101,210,215,1200],[310,415,3100,1315,101,310,415,3100],[410,15,4100,4115,1401,410,15,410],[510,515,1500,5115,5101,510,515,1500]; names = [:Scenario, :ID, :T1, :T2, :T3, :T4, :T5])
for k = 1:2
    Subset = select(filter(i -> (i.Scenario == k), DataT), All(Between(:T1, :T5)))
    ArrayX = Tables.matrix(Subset)
    end

```

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 25, 2020, 8:50pm UTC](https://discourse.julialang.org/t/array-and-table-not-updating-in-a-loop/36442/2 "2020-03-25T20:50:55Z")

</div>

```julia
using JuliaDB
using Tables
DataT = table([1,1,1,1,1,2,2,2], [1,2,3,4,5,1,2,3], [10,15,100,115,101,10,15,100],[210,215,1200,1215,101,210,215,1200],[310,415,3100,1315,101,310,415,3100],[410,15,4100,4115,1401,410,15,410],[510,515,1500,5115,5101,510,515,1500]; names = [:Scenario, :ID, :T1, :T2, :T3, :T4, :T5])
for k = 1:2
    global Subset = select(filter(i -> (i.Scenario == k), DataT), All(Between(:T1, :T5)))
    global ArrayX = Tables.matrix(Subset)
end

```

On 1.3.1 at least, not having the “global” declaration makes the variables local to the for loop…

---

<div class="post-metadata">

**Author:** ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Post date:** [March 26, 2020, 11:13am UTC](https://discourse.julialang.org/t/array-and-table-not-updating-in-a-loop/36442/3 "2020-03-26T11:13:02Z")

</div>

Thank you @pixel27

I have just a simple related question. How can I keep memory of all runs, i.e. table Subset and ArrayX for both k = 1 and 2?

If I set `ArrayX[k,:,:] ` it throws up the error below and I am not sure how I can set something for the table Subset.

```julia
using JuliaDB
using Tables
DataT = table([1,1,1,1,1,2,2,2], [1,2,3,4,5,1,2,3], [10,15,100,115,101,10,15,100],[210,215,1200,1215,101,210,215,1200],[310,415,3100,1315,101,310,415,3100],[410,15,4100,4115,1401,410,15,410],[510,515,1500,5115,5101,510,515,1500]; names = [:Scenario, :ID, :T1, :T2, :T3, :T4, :T5])
for k = 1:2
    global Subset = select(filter(i -> (i.Scenario == k), DataT), All(Between(:T1, :T5)))
    global ArrayX[k,:,:] = Tables.matrix(Subset)
end

```

`error DimensionMismatch("tried to assign 3×5 array to 1×5×5 destination")`

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 26, 2020, 12:38pm UTC](https://discourse.julialang.org/t/array-and-table-not-updating-in-a-loop/36442/4 "2020-03-26T12:38:50Z")

</div>

```julia
using JuliaDB
using Tables
DataT = table([1,1,1,1,1,2,2,2], [1,2,3,4,5,1,2,3], [10,15,100,115,101,10,15,100],[210,215,1200,1215,101,210,215,1200],[310,415,3100,1315,101,310,415,3100],[410,15,4100,4115,1401,410,15,410],[510,515,1500,5115,5101,510,515,1500]; names = [:Scenario, :ID, :T1, :T2, :T3, :T4, :T5])
Subsets = []
ArrayXs = []
for k = 1:2
    Subset = select(filter(i -> (i.Scenario == k), DataT), All(Between(:T1, :T5)))
    ArrayX = Tables.matrix(Subset)
    push!(Subsets, Subset)
    push!(ArrayXs, ArrayX)
end

```

Would be the easiest way…If you wanted to use types then:

```julia
ArrayXs = Vector{Array{64,2}}()
Subsets = Vector{IndexedTable}()

```

Although for Subsets that’s not the _exact_ type…that appears to be `IndexedTable{StructArrays.StructArray{NamedTuple{(:T1, :T2, :T3, :T4, :T5),NTuple{5,Int64}},1,NamedTuple{(:T1, :T2, :T3, :T4, :T5),NTuple{5,Array{Int64,1}}},Int64}}` which is a little long…

---

<div class="post-metadata">

**Author:** ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Post date:** [March 26, 2020, 1:02pm UTC](https://discourse.julialang.org/t/array-and-table-not-updating-in-a-loop/36442/5 "2020-03-26T13:02:51Z")

</div>

Thanks
