Array slice [1:<variable]


I am new to Julia Language aund I am struggling with following problem:

In my data array are empty elements and I am able to find the indices of these elements

> indices = findall(x → x == “”, data)

Next I want to slice the array in follwoing way (as an example the very first slice)

> upto = indices [1] - 1
> part = data[1:upto]

an error is reported: MethodError: no method matching getindex(::Symbol,::Int64)

Is it possible to specify a slice by variable(s)

Thanks a lot and best regards

Hello! I cannot reproduce your error, can you provide a full MWE?

julia> data = ["foo", "bar", "", "baz", "boo", "", "hoo"];
julia> indices = findall(x -> x == "", data)
2-element Vector{Int64}:
julia> upto = indices[1]-1;
julia> part = data[1:upto]
2-element Vector{String}:

However the error indicates, that you’re actually calling getindex on a symbol, maybe you overwrote your data variable with a symbol at some point?

julia> data = :foo

julia> data[1]
ERROR: MethodError: no method matching getindex(::Symbol, ::Int64)
 [1] top-level scope
   @ REPL[7]:1

Edit: completely wild guess but you might be also interested in

julia> data = ["foo", "bar", "", "baz", "boo", "", "hoo"];
julia> indices = findall(x -> x == "", data);
julia> deleteat!(data, indices)

which will remove all empty strings from your data array.

1 Like


thank you for your reply.

Shame on me! You’re abolutely right. It’s works as expected.

Excuse me for the unnecessary noise.

Best regards