I’m looking into calling functions with an input argument which is a string – normally of type:
s = "K = 5"
myfunction(s)
where myfunction should parse the string argument and then set K equal to the number 5 when calling some *.exe file.
However, sometimes I’d like to set K equal to a string. Thus, K = <some string>, where myfunction should parse the string and set K equal to the string…
OK… let me try to be clearer… myfunction will call another program where some label K is set to either a number (float) or a string. I want to transfer this information (label, value) in a string. For a float, it is simple, e.g., myfunction("K = 5") and the argument can be parsed. But what if the value is a string?
It looks like your suggestion is myfunction("K = \"some string\""). Is it simple to parse this and pick out \"some string\", and then convert this to a string?
julia> function myfun(;kwargs...)
for arg in kwargs
println("keyword = ",arg[1]," value = ",arg[2])
end
end
myfun (generic function with 2 methods)
julia> myfun(K=1,KK="test text",x="Another String")
keyword = K value = 1
keyword = KK value = test text
keyword = x value = Another String
julia> myfun(K=1)
keyword = K value = 1
julia> myfun(K="Some text")
keyword = K value = Some text
julia>
Not really. I’m looking form something like call myfun("K=5") should internally split the argument into ["K", 5], while myfun("K=\"some string\"") should internally split the argument into ["K", "some string"].
Perhaps a clarifying comment… the reason why myfun(K=5) or myfun(K="some string") won’t do, is that my example label K in reality can be an identifier (for another language) that is not a valid Julia identifier. This other language allows identifiers which could be "a.b", "d(T)", "a.d(T)", "a.b.d(T)", etc. And the assigned value may be a number or a string. Anyway, I assume I can figure out how to parse "K=5" vs. "K=\"some string\""…
As far as I know, myfun(a.b.d(T)=5) or myfun(a.b.d(T)="string") won’t work.
The label names come from this other language, so I am not at liberty to limit the identifiers to be valid Julia identifiers.
Why do you need to parse things into julia objects to then send them over to another program? And if you need to do that, what subset of julia do you need to support on the RHS of the assignment?
Sorry for not coming back to your comment. Turned out we could just split the string at the equality sign, and then convert to float if the string contained a number, and keep as string otherwise.