function F(a=parse(Float64,readline()))
@show a
end
F()
function F(a=parse(Float64,readline()))
@show a
end
F()
You already found readline
, which answers your first question.
It returns a string, which you have to process.
Look at ?parse
and ?tryparse
for parsing numbers.
To parse a vector, the best strategy is to split it to components (you could use Julia’s code parser, but that allows arbitrary code, so it is not recommended). Use split
or regular expressions.
Why not? It’s not like it actually evaluates the code.
julia> function parsevector(T, str)
expr = Meta.parse(str)
if expr.head == :vect
Vector{T}(expr.args)
else
error("could not parse")
end
end
julia> parsevector(Int, "[1, 2, 3]")
3-element Array{Int64,1}:
1
2
3
The way you did it is OK I think (because nothing else should be able to get through); I was thinking of converting with eval
.