# Access variable values

**URL:** <https://discourse.julialang.org/t/access-variable-values/34070>\
**Category:** New to Julia\
**Created:** [February 2, 2020, 12:34am UTC](https://discourse.julialang.org/t/access-variable-values/34070 "2020-02-02T00:34:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 2, 2020, 12:34am UTC](https://discourse.julialang.org/t/access-variable-values/34070/1 "2020-02-02T00:34:15Z")

</div>

Pardon my ignorance, but I have a very basic question.

Suppose I have a variable and I want to assign the value of that variable to  
the value of another variable without structurally linking the two variables  
(i.e., the assignment has only to do with numbers).

One way I was able to do this in Flux was when I used the ‘\<variable\_name\>.data’ option, which referred to the data in \<variable\_name\> but not the tracked details, etc.

What I am asking for is a different sort of assignment, the capability of setting a variable equal to the _value_ of another variable but not to the variable itself.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [February 2, 2020, 12:48am UTC](https://discourse.julialang.org/t/access-variable-values/34070/2 "2020-02-02T00:48:53Z")

</div>

I think it depends what exactly the variable is. If it’s an array, then you can use `copyto!` or broadcasted assignment:

```julia
julia> A = rand(4,4)
4×4 Array{Float64,2}:
 0.492154 0.497869 0.351142 0.7143
 0.519419 0.825479 0.393257 0.436978
 0.613145 0.0596596 0.944314 0.762933
 0.378408 0.312915 0.147394 0.00624961

julia> B = zeros(4,4)
4×4 Array{Float64,2}:
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0

julia> B .= A
4×4 Array{Float64,2}:
 0.492154 0.497869 0.351142 0.7143
 0.519419 0.825479 0.393257 0.436978
 0.613145 0.0596596 0.944314 0.762933
 0.378408 0.312915 0.147394 0.00624961

julia> A[1] = 2.0
2.0

julia> B
4×4 Array{Float64,2}:
 0.492154 0.497869 0.351142 0.7143
 0.519419 0.825479 0.393257 0.436978
 0.613145 0.0596596 0.944314 0.762933
 0.378408 0.312915 0.147394 0.00624961

julia> copyto!(B, A)
4×4 Array{Float64,2}:
 2.0 0.497869 0.351142 0.7143
 0.519419 0.825479 0.393257 0.436978
 0.613145 0.0596596 0.944314 0.762933
 0.378408 0.312915 0.147394 0.00624961

julia> B
4×4 Array{Float64,2}:
 2.0 0.497869 0.351142 0.7143
 0.519419 0.825479 0.393257 0.436978
 0.613145 0.0596596 0.944314 0.762933
 0.378408 0.312915 0.147394 0.00624961

julia> A[1] = 3.0
3.0

julia> B
4×4 Array{Float64,2}:
 2.0 0.497869 0.351142 0.7143
 0.519419 0.825479 0.393257 0.436978
 0.613145 0.0596596 0.944314 0.762933
 0.378408 0.312915 0.147394 0.00624961

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 2, 2020, 12:50am UTC](https://discourse.julialang.org/t/access-variable-values/34070/3 "2020-02-02T00:50:22Z")

</div>

Assignment **never** assign the variable, it always assign the value. In another word, if you do `a = b; b = c;` `b` will have the value of `c` while `a` won’t. (assuming `c` isn’t `b` to begin with).

What you are probably asking is an assignment that makes a **copy**. That is **not** an assignment, it’s a copy. It’s not a fundamental part of the language and supported mutable types should implement `copy` to do that so that you can do `a = copy(b)`. Immutable types generally don’t allow that.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 2, 2020, 10:11am UTC](https://discourse.julialang.org/t/access-variable-values/34070/4 "2020-02-02T10:11:03Z")

</div>

Thanks very much, that is exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 2, 2020, 10:13am UTC](https://discourse.julialang.org/t/access-variable-values/34070/5 "2020-02-02T10:13:04Z")

</div>

Thanks for that. I will try some experiments.

---

<div class="post-metadata">

**Author:** ![markushhh](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@markushhh](https://discourse.julialang.org/u/markushhh)\
**Post date:** [February 2, 2020, 11:14am UTC](https://discourse.julialang.org/t/access-variable-values/34070/6 "2020-02-02T11:14:10Z")

</div>

What about `deepcooy`?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 2, 2020, 9:14pm UTC](https://discourse.julialang.org/t/access-variable-values/34070/7 "2020-02-02T21:14:46Z")

</div>

It’s something else that’s completely unrelated to assignment and pretty much unrelated to `copy`. It’s usually not what you want.
