# Increase value of element in array. Like setindex!, but addition

**URL:** <https://discourse.julialang.org/t/increase-value-of-element-in-array-like-setindex-but-addition/48521>\
**Category:** New to Julia\
**Created:** [October 16, 2020, 9:21pm UTC](https://discourse.julialang.org/t/increase-value-of-element-in-array-like-setindex-but-addition/48521 "2020-10-16T21:21:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 16, 2020, 9:21pm UTC](https://discourse.julialang.org/t/increase-value-of-element-in-array-like-setindex-but-addition/48521/1 "2020-10-16T21:21:30Z")

</div>

Sometimes I have an array, and want to pass a copy of the array into a function, but updated, e.g.

```julia
function my_array_function(array)
    do_something_else_function(setindex!(copy(array),0.,1))
end

```

this is basically a shorthand for

```julia
function my_array_function(array)
    new_array = copy(array)
    new_array[1] = 0.
    do_something_else_function(new_array)
end

```

Howevever, sometimes I don’t know what the target index is, and simply want to increase it by some number, e.g. I want

```julia
function my_array_function(array)
    new_array = copy(array)
    new_array[1] += 1.
    do_something_else_function(new_array)
end

```

is there a similar

```julia
function my_array_function(array)
    do_something_else_function(addindex!(copy(array),0.,1))
end

```

to make this shorter?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [October 17, 2020, 3:53pm UTC](https://discourse.julialang.org/t/increase-value-of-element-in-array-like-setindex-but-addition/48521/2 "2020-10-17T15:53:37Z")

</div>

I take it that you don’t want to make your own `addindex!` function for some reason?

```julia
function addindex!(a, idx, v)
      b = copy(a)
      b[idx] += v
      return b
end

```

---

<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:** [October 17, 2020, 5:56pm UTC](https://discourse.julialang.org/t/increase-value-of-element-in-array-like-setindex-but-addition/48521/3 "2020-10-17T17:56:16Z")

</div>

Something like `updateindex!` has been discussed, see e.g. [https://github.com/JuliaLang/julia/issues/15630](https://github.com/JuliaLang/julia/issues/15630).

As of now you have to write your own little convenience function though.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 17, 2020, 8:15pm UTC](https://discourse.julialang.org/t/increase-value-of-element-in-array-like-setindex-but-addition/48521/4 "2020-10-17T20:15:05Z")

</div>

Thanks, both for the help 🙂
