# Unexpected "end"

**URL:** https://discourse.julialang.org/t/unexpected-end/17579
**Category:** General Usage
**Created:** [November 15, 2018, 9:34pm UTC](https://discourse.julialang.org/t/unexpected-end/17579 "2018-11-15T21:34:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [November 15, 2018, 9:34pm UTC](https://discourse.julialang.org/t/unexpected-end/17579/1 "2018-11-15T21:34:00Z")

</div>

Is this on purpose? I was clearly expecting that `end` could be used here.

```julia
julia> a=rand(3,3)
3×3 Array{Float64,2}:
 0.886505 0.270155 0.912865
 0.335745 0.6829 0.402649
 0.839621 0.736626 0.174028

julia> view(a, :, end)
ERROR: syntax: unexpected "end"

julia> view(a, :, size(a,2))
3-element view(::Array{Float64,2}, :, 3) with eltype Float64:
 0.9128648347755064
 0.4026488396914525
 0.17402766763129351

```

---

<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: [November 15, 2018, 9:34pm UTC](https://discourse.julialang.org/t/unexpected-end/17579/2 "2018-11-15T21:34:53Z")

</div>

Yes, either use `@view a[:, end]` or `view(a, :, lastindex(a))`.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [November 15, 2018, 9:38pm UTC](https://discourse.julialang.org/t/unexpected-end/17579/3 "2018-11-15T21:38:30Z")

</div>

OK, thanks. But the `lastindex(a)` errors

```julia
julia> view(a, :, lastindex(a))
ERROR: BoundsError: attempt to access 3×3 Array{Float64,2} at index [Base.Slice(Base.OneTo(3)), 9]
Stacktrace:
 [1] throw_boundserror(::Array{Float64,2}, ::Tuple{Base.Slice{Base.OneTo{Int64}},Int64}) at .\abstractarray.jl:492
 [2] checkbounds at .\abstractarray.jl:457 [inlined]
 [3] view(::Array{Float64,2}, ::Function, ::Int64) at .\subarray.jl:134
 [4] top-level scope at none:0

julia> @view a[:, end]
3-element view(::Array{Float64,2}, :, 3) with eltype Float64:
 0.9128648347755064
 0.4026488396914525
 0.17402766763129351

```

---

<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: [November 15, 2018, 9:40pm UTC](https://discourse.julialang.org/t/unexpected-end/17579/4 "2018-11-15T21:40:14Z")

</div>

Should be `lastindex(a, 2)`. A bit long, perhaps better to use the macro.
