Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

How to pass a relative UnitRange to a function?

$
0
0

I have the following which tries to apply a relative unit range to a string whose length is unknown:

function testnums(data::String,numrange::String)
    data[numrange]
end

foo = "asdfasdfasdf"
bar = "end-3:end"
testnums(foo,bar)
Here is the error ``` MethodError: no method matching getindex(::String, ::String) Closest candidates are: getindex(::String, !Matched::UnitRange{Int64}) at strings/string.jl:245 getindex(::String, !Matched::Int64) at strings/string.jl:210 getindex(::String, !Matched::UnitRange{#s69} where #s69<:Integer) at strings/string.jl:242 ... testnums(::String, ::String) at passargs.jl:2 top-level scope at passargs.jl:7 ```

As suggested by the error, you can’t pass a string for the range. So how can I pass a relative range eg end-3:end?

9 posts - 7 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images