On Fri, 2014-11-14 at 04:56, Amuthan A. Ramabathiran <apar...@gmail.com> wrote:
> Hello, 
>
> I ran into this problem recently: 
>
> julia> a = Array{Int,1}[]
> 0-element Array{Array{Int64,1},1}
>
> julia> b = zeros(Int,2)
> 2-element Array{Int64,1}:
>  0
>  0
>
> julia> b[1] = 1
> 1
>
> julia> push!(a,b)
> 1-element Array{Array{Int64,1},1}:
>  [1,0]
>
> julia> b[1] = -1
> -1
>
> julia> push!(a,b)
> 2-element Array{Array{Int64,1},1}:
>  [-1,0]
>  [-1,0]
>
>
> I'm wondering why a[1] changes to [-1,0]. Is it because only a reference to 
> b is stored when it is pushed to a? Are there any simple solutions to avoid 
> this?

Correct.  You can copy b before inserting:

push!(a,copy(b))

I think there is an entry in the FAQ about mutability.

Reply via email to