# Wrong order in push! operator

**URL:** https://discourse.julialang.org/t/wrong-order-in-push-operator/50086
**Category:** General Usage
**Created:** [November 13, 2020, 10:20am UTC](https://discourse.julialang.org/t/wrong-order-in-push-operator/50086 "2020-11-13T10:20:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![FrootLoops](https://avatars.discourse-cdn.com/v4/letter/f/c4cdca/32.png) [@FrootLoops](https://discourse.julialang.org/u/FrootLoops)
#### Post date: [November 13, 2020, 10:20am UTC](https://discourse.julialang.org/t/wrong-order-in-push-operator/50086/1 "2020-11-13T10:20:14Z")

</div>

Hi,  
I have a problem using the push! operator. It seems that it does not push the next elements at the end of the arrays. Can someone confirm my result:

```julia
using Distributions

e = 1 * 10^(-2)
n_data = 2

x = rand(Uniform(-e, e), n_data, 1)
y = rand(Uniform(-e, e), n_data, 1)
z = rand(Uniform(-e, e), n_data, 1)

vec1 = []
vec2 = []

for xi in x
 a = zeros(3)
 a[1] = xi
 for yi in y
     a[2] = yi
     for zi in z
         a[3] = zi
         b = 2 * a
         push!(vec1, a)
         push!(vec2, b)

     end
 end
end

println(vec1[1])
println(vec2[1])

```

The output I get is :

[-0.007628929670578818, -0.007324861395442884, 0.003913249936206481]  
[-0.015257859341157635, -0.015167500156704537, -0.014534791007813352]

which is not corresponding with b = 2\*a.

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [November 13, 2020, 10:47am UTC](https://discourse.julialang.org/t/wrong-order-in-push-operator/50086/2 "2020-11-13T10:47:47Z")

</div>

The problem is that the vector `a` is mutated in the two inner loops (only the outermost loop creates a new vector every time). Consider the following example:

```julia
julia> v = [];

julia> a = [1,2,3];

julia> push!(v, a)
1-element Array{Any,1}:
 [1, 2, 3]

julia> a[3] = 100
100

julia> push!(v, a)
2-element Array{Any,1}:
 [1, 2, 100]
 [1, 2, 100]

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 13, 2020, 10:54am UTC](https://discourse.julialang.org/t/wrong-order-in-push-operator/50086/3 "2020-11-13T10:54:54Z")

</div>

See this post on [push!](https://discourse.julialang.org/t/using-push/30935/4)
