In which coding languages can variable names be declared as strings?

I find to know a coding langauge where vars can be declared by strin
Repl link:

var "variable_name" = "value"


"variable_name" = "value"

or something like that I’m not looking for anything specific

Hi,@SnakeyKing. Welcome to the forums!

In most commonly used programming languages like Python, Java, C++, and JavaScript, variable names cannot be declared as strings.

However, some interpreted languages like Ruby and Perl allow variable names to be declared and accessed as strings. Instead of using traditional variable naming conventions, such as starting with a letter and not using spaces or special characters, these languages allow developers to create variable names as strings, which can be dynamically created and accessed at runtime.

For example, in Ruby, you can define a variable with a string name using the following syntax:

my_var_name = "my_variable"
my_hash = {}
my_hash[my_var_name] = 100
puts my_hash[my_var_name]

This creates a variable named “my_variable” and sets its value to 100, which is then printed to the console. Note that this syntax is not recommended in most cases as it can lead to hard-to-debug errors and make code harder to read and understand.

you may mark this as the solution.

1 Like

It doesn’t work (in Ruby)

That just adds an item to a dictionary.

Why do you even want to do that? A dictionary should be the solution for such problems.

Even if it is a bad practice to do this, here you go:

exec("VarName = 'something else'")

If you have the var name in variable:

var_name = "index"
exec(f"{var_name} = 'another sting'")

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.