concatenate variable names

Greetings. The query is the following:

I have the variable $hello with the content 123
I want to define a variable whose name is the content of $hello + 456
if I did a put it would be :put $hello+123
the name of the final variable should be :123456
Is it possible? How is it done?

Completely useless or poorly engineered problem.


There are already dozens of posts, many of which I wrote, that explain the exact same thing, use the search function.

Just use a 2nd variable.

:global hello 123
:global hello2 "$($hello)456"
:put $hello2



123456

But this is not what OP ask: concatenate variable names

Your example is not concatenate variable names, but concatenate values

Is better define the problem: Define a variable whose name is the concatenation of the content of another two variables parts.

And the solution is: change mind and use one array.


I do not use 123 on example for avoid confusion on variable name that is one number.
{
:local variables {“hello”=“abc”}
:put “current content of hello variable is >$($variables->“hello”)<”
:local newvarname (($variables->“hello”) . “456”)
:put “new variable name is >$newvarname<”
:set ($variables->$newvarname) “test”
:put “value of >$newvarname< is >$($variables->$newvarname)<”
}
Results:

current content of hello variable is >abc<
new variable name is >abc456<
value of >abc456< is >test<

Typical error. Asking how to solve brainfart instead of describing the problem to solve and get the real solution.
We don’t know what OP wants to solve, but i suspect that a classical hashmap is what OP want.

Well I'm not sure OP was very clear. :wink: String interpolation get's tricky with variables and numbers... i.e. "$($var123)456" syntax isn't entirely obvious too.

Maybe OP did not hear for key-value arrays (maps/dictionaries) where to some dynamically created key (identifier) some value can be assigned, only logic I can think of for such request…

The title is cofusing but the content is clear and understandable.

It’s clear: the name of the final variable should be :123456 :slight_smile:

I’m more curious what underlying problem got someone to think concatenating an actual variable name is the solution… :wink:

:local hello 123
:local hellos [:toarray ""]
:set ($hellos->"$([:tostr $hello])") $hello
:set ($hellos->"$([:tostr $hello])456") $hello
:put ($hellos->"123")
:put ($hellos->"123456")

:slight_smile:

Similar what @rextended did in #4 just value type and naming adjusted as in OP
Still this needs more context…

For do the same with a variable called 123456 (ignoring the : that is a typo for sure…)
{
:local variables {“hello”=“123”}
:put “current content of hello variable is >$($variables->“hello”)<”
:local newvarname (($variables->“hello”) . “456”)
:put “new variable name is >$newvarname<”
:set ($variables->$newvarname) “test”
:put “value of >$newvarname< is >$($variables->$newvarname)<”
}

current content of hello variable is >123<
new variable name is >123456<
value of >123456< is >test<

As I already write: I do not use 123 on example for avoid confusion on variable name that is one number.


Whatever happens, I already explained in 2021 how to do it in an unnecessarily complicated way…
http://forum.mikrotik.com/t/concatenate-values-to-create-variable-name/151793/2