variables in string

Hi All:
for the example below:

:local testStr "198"
:local testStr2 "199"

:log warn "$testStr2"

If i want get the result of 1982(not 199),how to do it?
I know it can be done like:

:log warn ($testStr . "2")

but i wonder if there is an easier way.
Thank you.

You ask seriously? You want 1982?

:log warn "1982"

First of all, it is of no use whatsoever, or if it is, you have been very careful not to write it.

Are you kidding?
It is obvious that if you write a valid variable name, it goes and looks for that variable name.
How do you think?

Other languages offer you string interpolation of variables like this:

:log warn "${testStr}2"

or

:log warn "{$testStr}2"

Of course not working in ROS scripting. First example is syntax error and the second one works, but outputs the braces. I guess OP does not want the braces.

Try:

:log warn "$(testStr)2"

Regardless of example. It is a valid question to ask. I guess the stupid example is just to better understand OPs question. I don’t want to see/understand complicated real-life code for just answering such a simple question. So, colin, all fine with your sample.

Why “Try”?
You can’t try yourself before posting other oddities?

without add too much complexity, you have 2 solutions:

:local testStr "198" ; :log warn "$($testStr)2"

:local testStr "198" ; :log warn ($testStr."2")

extra:

:local testStr "198" ; :log warn "$testStr$""2"

So, the “easier way” do not exist.

EDIT: I just noticed that all my “solutions” only add 3 more characters to the “base” each time…

Lol. Sry Mr rextended for polite wording.

“Use” instead of “Try”. Because I actually verified and tried myself before posting. But on ROS 7.15.3 tested. OP did not say which ROS version. Can’t say if it is compatible with v6 or older V7. Now understand the term “Try”?

(orignal post edited later)

Sorry, but :local testStr “198” ; :put “$(testStr)2” print only 2, on both v6 and v7… (6.48.7 // 7.15.3) $ must be present inside the parenthesis.

I dont know what’s wrong with the parser, but with a global var it works fine.
2024-08-23_13-26.png

I do not use global, because on OP is using locals. I test, one moment please.

what t. f. ???[admin@TEST-v7] > :local testStr “198” ; :put “$(testStr)2”
2
[admin@TEST-v7] > :local teststr “198” ; :put “$(teststr)2”
1982
if the var name containing one at least one upper case… do not work…


Probably because this (is for the array):

Warning: Key name in array contains any character other than lowercase character, it should be put in quotes

Ok, parser error apart, there is no way (except the 3 + 1 proposed) to not write more code for print vars followed from something fixed.

OMFG. Latvian parser.

fix: if the var name containing one at least one upper case… do not work…

hashtag #nocamelcase :wink:

https://en.wikipedia.org/wiki/Camel_case

:laughing:

How do I output a variable named in snake case?

:global "foo_bar" "baz"

This simple :put does not work:

:put "$foo_bar"

like the previous 3 examples:

:local "test_Str" "198" ; :log warn "$($"test_Str")2"

:local "test_Str" "198" ; :log warn ($"test_Str"."2")

:local "test_Str" "198" ; :log warn "$"test_Str"$""2"

Awesome! It works. The solution with double quotes is really universal.

Notice: " must not be escaped on string, is parsed first $“…” than the rest of the string.

eg:

:log warn “$“test_Str”$”“2”
$“test_Str” is replaced with his contents 198 and $“” do not exist, so, is replaced with string of zero length (that is not the same as a string with 0x00 character inside) =>
:log warn “1982”
so, is logged only 1982 =>
:log warn “1982”

For curiosity, the first two soluctions do the same result once parsed from “compiler” (concat $test_Str and 2):
(evl /localname=$test_Str;value=198);(evl /log/warningmessage=(. $test_Str 2))

the third, just add empty string (the $ ) to the code (concat $test_Str and $ and 2):
(evl /localname=$test_Str;value=198);(evl /log/warningmessage=(. $test_Str $ 2))

So, for script efficency, is better the first two (on this case) and not the third.

:local testStr2 "199"; :log warn "$($testStr2)8"
:local "test_Str2" "199"; :log warn "$($"test_Str2")8"

or masochism:

:local "test_Str2" "199"; :log warn "$($"test_Str2")$(:put (4+4))"

:slight_smile:

It’s why my reply to OP on post #2:wink:

I’m not good at english, so sorry for my bad example.
But i think @infabo understood what i mean, and i got the answer from other replies.
So thanks to all.

Here is more to study :slight_smile::

:local testStr2 "199"; :log warn "$($testStr2)$([:local num; while ($num = null) do={:local r [:rndnum from=[:len [:pick $testStr2 3]] to=[:tonum [:pick $testStr2 2]]]; :if ($r = (([:tonum [:pick $testStr2 0]])^([:tonum [:pick $testStr2 1]]))) do={:set num $r}}; $num])"