Possible to parse strings containing strings?

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…

How can this be done?

Hm, what do you mean when calling “some .exe file”?

Anyway, it seems you just want to escape the quotes, e.g. "K = \"string\""?

Maybe you already know, but just in case:

run(`some.exe`)

Note the command ticks inside run.

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?

"K = \"some string\""

is the string with the content K = "some string" so should be fairly easy to parse that.

Maybe you are looking something like:

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"].

That is exactly what my code demonstrates. arg in my code is your ["K",1] in yours.

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.

Just use split.

Yes… I tried with the following:

function myparse(arg)
   splt_arg = split(arg,"=")
   spl2 = try parse(Float64,splt_arg[2])
   catch
       split(splt_arg[2],"\"")[2]
    end
    return [splt_arg[1], spl2]
end

Test:

julia> stra = "K=5"
julia> strb = "a.d(T) = \"test string\""
julia> myparse(stra)
2-element Array{Any,1}:
  "K"
 5.0
julia> myparse(strb)
2-element Array{SubString{String},1}:
 "a.d(T) "
 "test string"

Probably not very elegant code… and I should trim off some spaces.

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.