# Update array

**URL:** <https://discourse.julialang.org/t/update-array/92530>\
**Category:** General Usage\
**Created:** [January 5, 2023, 12:25am UTC](https://discourse.julialang.org/t/update-array/92530 "2023-01-05T00:25:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgr](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@jgr](https://discourse.julialang.org/u/jgr)\
**Post date:** [January 5, 2023, 12:25am UTC](https://discourse.julialang.org/t/update-array/92530/1 "2023-01-05T00:25:55Z")

</div>

Hi, I am trying to update an array using the following code

```julia
add!(z::Val{1}, list, a) = add!(list, a)

 function add!(list, a)
    list = list .+a
   return nothing
 end

```

However, this does not seem to work well:

```julia
list = [1, 2]
a = 1
add!(Val(1), view(list,:), a)
list

```

The `list` array does not change to [2,3], I was wondering if anyone knows what is wrong with my code. Thanks!

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 5, 2023, 12:29am UTC](https://discourse.julialang.org/t/update-array/92530/2 "2023-01-05T00:29:58Z")

</div>

Try either

```julia
 function add!(list, a)
    list .= list .+a
   return nothing
 end

```

or

```julia
 function add!(list, a)
    list .+= a
   return nothing
 end

```

Here’s a demo:

```julia
julia> function add!(list, a)
           list .= list .+a
          return nothing
        end
add! (generic function with 2 methods)

julia> add!(Val(1), view(list,:), a)

julia> list
2-element Vector{Int64}:
 2
 3

```

The reason your version does not work is that `list .+ a` creates a new array, and this array is bound the local variable `list`, abandoning the array previous bound to `list`. However, the new `list` is not returned.

My version updates each element of the original array `list` by using `.=` or `.+=`. I think this is what you want based on the use the exclamation mark.

---

<div class="post-metadata">

**Author:** ![jgr](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@jgr](https://discourse.julialang.org/u/jgr)\
**Post date:** [January 5, 2023, 12:33am UTC](https://discourse.julialang.org/t/update-array/92530/3 "2023-01-05T00:33:40Z")

</div>

That works, thanks! Actually, `list = list.+a` outside the function works well, do you happen to know why inside function it fails?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 5, 2023, 12:37am UTC](https://discourse.julialang.org/t/update-array/92530/4 "2023-01-05T00:37:55Z")

</div>

I added an explanation above. Within the function, you are only changing the local variable `list`. Outside the function `list` refers to the global variable `list`.

When you use `list = list .+ a` you are creating a new array on the right hand side, and then binding that new array to `list`. Within the function, `list` is a local variable which is discarded. You would have to specify `global list` to refer to the global variable. Outside the function, you updating the global binding `list` to the new array created on the right hand side. The array previously bound to `list` is discarded.

---

<div class="post-metadata">

**Author:** ![jgr](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@jgr](https://discourse.julialang.org/u/jgr)\
**Post date:** [January 5, 2023, 12:40am UTC](https://discourse.julialang.org/t/update-array/92530/5 "2023-01-05T00:40:38Z")

</div>

I see, thanks!
