# Why is Julia messing up my variables?

**URL:** <https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289>\
**Category:** New to Julia\
**Created:** [December 15, 2019, 4:19am UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289 "2019-12-15T04:19:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lko](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@lko](https://discourse.julialang.org/u/lko)\
**Post date:** [December 15, 2019, 4:19am UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/1 "2019-12-15T04:19:29Z")

</div>

Can one of you explain to me what is going on here and how to avoid it? Can you reproduce this behaviour?

When I define x as

```julia
x = zeros(10)
x .= .05
x 

```

I get

```julia
Vector{Float64} with 10 elements
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500

```

when I assign x to y:

```julia
y = x
x

```

I still get

```julia
Vector{Float64} with 10 elements
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500

```

but when I assign some values to some elements of **y** , my **x** values suddenly get messed up:

```julia
y[(1:10).<7] .= 1
x

```

gives

```julia
Vector{Float64} with 10 elements
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
0.0500
1.00
1.00
1.00

```

Can one of you explain me what I’m doing wrong here?

```julia
# Atom:
Version: 1.41.0
Dev Mode: false
Official Release: true
{
  "http_parser": "2.8.0",
  "node": "10.11.0",
  "v8": "6.9.427.31-electron.0",
  "uv": "1.23.0",
  "zlib": "1.2.11",
  "ares": "1.14.0",
  "modules": "69",
  "nghttp2": "1.33.0",
  "napi": "3",
  "openssl": "1.1.0",
  "electron": "4.2.7",
  "chrome": "69.0.3497.128",
  "icu": "62.2",
  "unicode": "11.0",
  "cldr": "33.1",
  "tz": "2019a"
}
# julia-client:
Version: 0.11.3
Config:
{
  "firstBoot": false,
  "uiOptions": {
    "enableMenu": true,
    "enableToolBar": true
  }
}

# ink:
Version: 0.11.7
Config:
undefined

# uber-juno:
Version: 0.3.0
Config:
{
  "disable": true
}

# language-julia:
Version: 0.19.2
Config:
undefined

# language-weave:not installed

# indent-detective:
Version: 0.4.0
Config:
undefined

# latex-completions:
Version: 0.3.6
Config:
undefined

# versioninfo():
Julia Version 1.3.0
Commit 46ce4d7933 (2019-11-26 06:09 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 4

    Status `/home/local/kohlluka/.julia/environments/v1.3/Project.toml`
  [c52e3926] Atom v0.11.3
  [e5e0dc1b] Juno v0.7.2

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 15, 2019, 4:41am UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/2 "2019-12-15T04:41:29Z")

</div>

Julia like most other high level languages (oppose C) think of variables as pointers to data. Thus `y=x` doesn’t mean “make y hold a copy of x”, but instead means “make y a synonym for x”. Thus changing y, naturally, changes x. If you want a copy, you should instead write `y=copy(x)`.

---

<div class="post-metadata">

**Author:** ![lko](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@lko](https://discourse.julialang.org/u/lko)\
**Post date:** [December 15, 2019, 4:52am UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/3 "2019-12-15T04:52:34Z")

</div>

thanks for your help!

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [December 15, 2019, 6:07am UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/4 "2019-12-15T06:07:27Z")

</div>

In addition, you may also find the behavior shown below helpful to keep in mind as a reference. Array slicing (things like `a[:]` where `a` is an array) also copies the underlying vector.

```julia
########################
       Example 1
########################

julia> a = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

#creates a copy of a so changing b won't change a
julia> b = a[:]
3-element Array{Int64,1}:
 1
 2
 3

julia> b[1]
1

julia> b[1] = 4
4

julia> b
3-element Array{Int64,1}:
 4
 2
 3

julia> a
3-element Array{Int64,1}:
 1
 2
 3

########################
      Example 2 
########################

julia> a = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

#b is synonymous with a so changing b will change a
julia> b = a
3-element Array{Int64,1}:
 1
 2
 3

julia> b[1]
1

julia> b[1] = 4
4

julia> b
3-element Array{Int64,1}:
 4
 2
 3

julia> a
3-element Array{Int64,1}:
 4
 2
 3

```

For more information, I would encourage reading “Gotcha #5: Views, Copy and Deepcopy” in

> **[7 Julia Gotchas and How to Handle Them - Stochastic Lifestyle](https://www.stochasticlifestyle.com/7-julia-gotchas-handle/)**
>
> Let me start by saying Julia is a great language. I love the language, it is what I find to be the most powerful and intuitive language that I have ever used. It’s undoubtedly my favorite language. That said, there are some “gotchas”, tricky little...

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [December 16, 2019, 3:21am UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/5 "2019-12-16T03:21:37Z")

</div>

What is the advantage of using pointers to same data when we write `b = a`? When we create a new variable with the value of old, it is usually to modify the data in the new one. Even though I have seen this behavior in other languages also, I’m yet to understand why such a “confusing” behavior is so widespread in many languages?

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [December 16, 2019, 3:28am UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/6 "2019-12-16T03:28:36Z")

</div>

The behavior is useful because copying data can be expensive. Saying `b = a` avoids a copy. Moreover, it’s sometimes useful to keep the same array in different forms _without_ copying the underlying data. Again, the behavior of `b = a` let’s you achieve that.

See the following quote from [7 Julia Gotchas and How to Handle Them - Stochastic Lifestyle](https://www.stochasticlifestyle.com/7-julia-gotchas-handle/)

"(Such behavior) is very useful because it also allows you to keep the same array in many different forms. For example, we can have both a matrix and the vector form of the matrix using:

```julia
a = rand(2,2) # Makes a random 2x2 matrix
b = vec(a) # Makes a view to the 2x2 matrix which is a 1-dimensional array

```

Now “b” is a vector, but changing “b” still changes “a”, where “b” is indexed by reading down the columns. Notice that this whole time, no arrays have been copied, and therefore these operations have been excessively cheap (meaning, there’s no reason to avoid them in performance sensitive code)."

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [December 16, 2019, 3:43am UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/7 "2019-12-16T03:43:40Z")

</div>

@bashonubuntu Thanks a lot for the explanation. The example you provided makes the reason more clear. 🙂

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [December 16, 2019, 3:44am UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/8 "2019-12-16T03:44:38Z")

</div>

Glad to hear!

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [January 16, 2020, 4:28pm UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/9 "2020-01-16T16:28:11Z")

</div>

But then why would you need to make b = a and not just keep using a?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 16, 2020, 4:31pm UTC](https://discourse.julialang.org/t/why-is-julia-messing-up-my-variables/32289/10 "2020-01-16T16:31:12Z")

</div>

`b = a` is quite rare to write but consider

```julia
b = [[1,2,3], [4,5,6]]
a = b[1]

```

or passing a variable into a function.
