Page 1 of 1

Feed in array from command line as -g parameter

Posted: 17 Feb 2021, 18:16
by RobAtLab
Is there a way to do this? I know you can feed in strings directly
./vrep.sh -gWORD file.ttt
and
Var=sim.getStringParameter(sim.stringparam_app_arg1 )
print(Var)

Will print the string within V-REP.
and something like
./vrep.sh -g8961.4 file.ttt
along with
VarNum=tonumber(sim.getStringParameter(sim.stringparam_app_arg1))
can introduce a float or int value to the sims.

But what about feeding in an array, a 2D one, something like
{{5,4},{3,28},{16,93},{6,842},{91,0}}
within V-REP I can say
VarArr={{5,4},{3,28},{16,93},{6,842},{91,0}}
to make an array, but what is the equivalent of tonumber() when providing this array via a -g flag from the command line.

Please note, in my situation the array is "rectangular" (all the inner elements have the same, and a known, number of elements in them, 2 in this case), but the number of those inner arrays (5 in this exmaple) is variable and I might want to feed in arrays ranging from the likes of:
{{1,9},{2,7}}
right up to some holding maybe 50 inner arrays.
{{85,3},{3,6},...for a further 46...,{92,92},{8,17}}
Thank you

Re: Feed in array from command line as -g parameter

Posted: 17 Feb 2021, 20:53
by fferri
You might want to consider using a serialization format (e.g. JSON) to encode your data into a single value to be passed as a CoppeliaSim argument (e.g. -g"[[1,2],[3,4]]").

Use dkjson (bundled with CoppeliaSim) to decode the value into a Lua table:

Code: Select all

> json=require'dkjson'
> encodedData='[[1,2],[3,4]]' -- data returned by sim.getStringParameter(sim.stringparam_app_arg1)
> data=json.decode(encodedData)
> data
{{1, 2}, {3, 4}}
But keep in mind a command line has a maximum length which is platform dependent.

If your data is big'ish you might exceed that limit; in that case consider storing your data into a file.

Re: Feed in array from command line as -g parameter

Posted: 17 Feb 2021, 21:15
by RobAtLab
is use of [square brackets] as a substsitute for {curly ones} always standard across json then? Meaning all I have to do is supply my command line argument in square brackets ( should -g"[[5,6],[7,18]]" be valid on a linux command line?) to feed an array in? I am using an older V-REP version though, are json.decode and require 'dkjson' available for use in lua child scripts in V-REP 3.5, or must I update to CoppelliaSim? Thanks

Re: Feed in array from command line as -g parameter

Posted: 18 Feb 2021, 10:03
by fferri
I'm not using square brackets as substitute for curly ones.

Square brackets denote arrays. Curly braces denote objects. It's the JSON spec. Check out www.json.org

Lua on the other hand uses curly braces also for arrays.

Re: Feed in array from command line as -g parameter

Posted: 19 Feb 2021, 16:57
by RobAtLab
Thanks, it works great