Getting name from "name"

Is there a way in Julia to parse “name” into name?

I can use a Dict, but I don’t want to define every possible name in advance.

What are you trying to do/avoid? This is usually an indication you could structure your code better.

There are lots of caveats with this approach, but:

julia> x = "Hello. World!"
"Hello. World!"

julia> y = "x"

julia> getfield(@__MODULE__, Symbol(y))
"Hello. World!"

julia> eval(Symbol(y))
"Hello. World!"
1 Like

I’m simply trying to fetch the contents of a string. No ulterior motive involved.

It’d be simple is parse(string,Any) worked, but it doesn’t.

Both of these examples return strings, not the contents of the string.

Do you mean this?

julia> eval(Meta.parse("1 + 1"))

What do you mean by that? A string is a string, the only thing close to its contents are the characters making up that string.

Are you talking about referencing a variable based on a string? As indicated by @odow, this usually signifies a problem in your approach, if you can tell us a little bit more about what you’re trying to do we can probably suggest an alternative that’ll work better for your usecase.


No, I want to get 1+1 from “1=1”.

I gather the answer to my question is NO.

I’m afraid I don’t follow - what exactly are you referring to when you say that you want to get 1+1 from "1+1"? Are you referring to the expression 1+1? You can get that by doing Meta.parse("1+1"), though I doubt that will get you farther with your original goal, whatever that may be.“name”) yields :name — which may be useful. Thanks!

Again - what would you like to do with that? It very much seems like you’re trying to access some variable by a string, in which case a Dict is more appropriate than trying to get real declared variable values from a string. At the time the code runs, the variable name doesn’t exist anymore, as it’s compiled away.

What do you mean by 1+1? Do you want that to be evaluated or do you want an expression that can be evaluated?

odow’s answer was the solution for both of those things. Meta.parse gives you the expression :(1 + 1), which can be evaluated using eval.

Meta.parse("name") will give you :name sure, but that’s just a symbol, and isn’t attached to any variable. Using eval on that symbol will be in the global scope, which is typically not what you want.

But it’s important to know why you want to do this, because it’s typically a pretty bad idea.

As I said in in earlier post, using Dict requires me to name all potential names in advance.
I don’t want to do that, as it prevents using new names at run-time.
Thanks again for your replies.

I’m implementing a new language in Julia, for which I need the ability to extract the contents of a string.

Ok, but using Meta.parse will use the julia parser and thus you’ll be limited to julia expressions. Implementing your own language would require writing your own parser, which would be distinct from the julia parser.

Where do I find info on writing my own parser?

That sounds like fun! But note once again that eval will be in the global scope (you can do some tricky things to get around this by defining functions, but I’m not experienced with them), so it is really not recommended for a lot of things, and can often have really subpar performance. Others will have a better idea of what to do here.

E: In the end you may have to use eval in some fashion once you’ve parsed your new language, but that’s something to figure out later.

Since it seems like this is a new field for you, I’ll recommend a basic introduction:

as well as an introduction to bottom up parsing:

Be aware though, this is a larger topic than you may think. There are quite a few more introductions and tutorials on how to write a parser/compiler, but those two are good starting points for finding out what a parser is, as well as how you could do that.

Be aware that OP is not asking about parsing julia code. eval and Meta.parse will NOT be of help here. They’re trying to create their own programming language. Please don’t confuse the two.


Thanks again for your help! I watch those videos now.