Unexpected "end"

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

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

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

3 Likes

OK, thanks. But the lastindex(a) errors

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

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

1 Like