# How does copying slices work?

**URL:** <https://discourse.julialang.org/t/how-does-copying-slices-work/101777>\
**Category:** General Usage\
**Created:** [July 19, 2023, 12:09pm UTC](https://discourse.julialang.org/t/how-does-copying-slices-work/101777 "2023-07-19T12:09:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 19, 2023, 12:09pm UTC](https://discourse.julialang.org/t/how-does-copying-slices-work/101777/1 "2023-07-19T12:09:04Z")

</div>

```julia
julia> data=[1,2,3,4,5,6,0,0,0]
9-element Vector{Int64}:

julia> data[6:9].=data[3:6]

julia> data
9-element Vector{Int64}:
 1
 2
 3
 4
 5
 3
 4
 5
 6

```

why in this example data[9] =6 and not =data[3]?  
I’m fine with it working like this, of course, I just want to understand how this result is achieved.

same question for

```julia
copyto!(data,6,data,3,4)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 19, 2023, 12:37pm UTC](https://discourse.julialang.org/t/how-does-copying-slices-work/101777/2 "2023-07-19T12:37:35Z")

</div>

Specifically there `data[3:6]` is materialized into a new array first, and then the elements are copied (views in Julia copy). That is identical to:

```julia
julia> data2 = data[3:6]
4-element Vector{Int64}:
 3
 4
 5
 6

julia> data[6:9] .= data2
4-element view(::Vector{Int64}, 6:9) with eltype Int64:
 3
 4
 5
 6

julia> data
9-element Vector{Int64}:
 1
 2
 3
 4
 5
 3
 4
 5
 6

```

Another question is how this is achieved:

```julia
julia> data=[1,2,3,4,5,6,0,0,0];

julia> data[1:9] .= @view(data[9:-1:1])
9-element view(::Vector{Int64}, 1:9) with eltype Int64:
 0
 0
 0
 6
 5
 4
 3
 2
 1

```

At some point that ought to need some intermediate.

---

<div class="post-metadata">

**Author:** ![told](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/told/32/17083_2.png) [@told](https://discourse.julialang.org/u/told)\
**Post date:** [July 19, 2023, 12:38pm UTC](https://discourse.julialang.org/t/how-does-copying-slices-work/101777/3 "2023-07-19T12:38:28Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `data[6:9].=data[3:6]`

because

```julia
julia> data[6:9]
4-element Vector{Int64}:
 6
 0
 0
 0

```

and

```julia

julia> data[3:6]
4-element Vector{Int64}:
 3
 4
 5
 6

```

so the `.=` will assign those values _elementwise_. See the docs on [Vectorized “dot” operators](https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-dot-operators)

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [July 19, 2023, 12:38pm UTC](https://discourse.julialang.org/t/how-does-copying-slices-work/101777/4 "2023-07-19T12:38:48Z")

</div>

`data[6:9].=data[3:6]`

is equivalent to:

```julia
data[6] = data[3]
data[7] = data[4]
data[8] = data[5]
data[9] = data[6]

```

As `data[6]` is `9`, the new 9th position of data is 6.

EDIT: you are right, I didn’t look at the problem properly…

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [July 19, 2023, 12:44pm UTC](https://discourse.julialang.org/t/how-does-copying-slices-work/101777/5 "2023-07-19T12:44:26Z")

</div>

I don’t think your argument is correct, or I misunderstood it (following it we’d have `data[9] <- data[6] <- data[3]` which is not the case and that’s what OP was asking.

@lmiq is right that `data[3:6]` is materialised first and then the assignment is done for the 3 entries.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 19, 2023, 12:45pm UTC](https://discourse.julialang.org/t/how-does-copying-slices-work/101777/6 "2023-07-19T12:45:49Z")

</div>

Note that this is not the case:

```julia
julia> data = [1,2,3];

julia> data = @view(data[3:-1:1])
3-element view(::Vector{Int64}, 3:-1:1) with eltype Int64:
 3
 2
 1

julia> data = [1,2,3];

julia> data[1] = data[3]
       data[2] = data[2]
       data[3] = data[1]
       data
3-element Vector{Int64}:
 3
 2
 3

```

Semantically, the broadcast will work as if the array on the right is first copied into a new array (I’m not sure if non-copying is guaranteed always, I guess not, sometimes an intermediate is needed).

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 19, 2023, 12:46pm UTC](https://discourse.julialang.org/t/how-does-copying-slices-work/101777/7 "2023-07-19T12:46:29Z")

</div>

the result could be obtained with an “algorithm” equivalent to this

```julia
data[9] = data[6]
data[8] = data[5]
data[7] = data[4]
data[6] = data[3]

```
