I'd like the user to be able to provide a comma separated string as input,
e.g., "2,4,11,8" and then use that information inside a nested For loop to
decide whether to perform an action. For example, if the sum of the two
loop counters is one of the numbers in the string then don't do the action.
I've tried using str() and search() and have gotten close but the "11" above
is matching with i+j = 1.
Any suggestions?
Ken
--
Sent from: http://forum.openscad.org/
Make the string ",2,4,11,8," (add beginning and trailing commas), then search
for str(",", number, ",").
If you don't have control of the input string format, you can easily add the
commas in code with str(",", mysearchstring, ",").
--
Sent from: http://forum.openscad.org/
I understand your problem that you want "parse" a string containing a
comma-separated list of numbers into a vector. In short "2,4,11,8" ->
[2,4,11,8].
This is not so trivial. See the following code.
a = "2,4,116,8"; // input
nums = "0123456789"; // helper
echo(parse(a));
function parse() = let(comma = [-1, each search(",", a, 0)[0], len(a)])
[for(i=[0:len(comma)-2]) vec2num(digitvector(comma, i))];
function digitvector(comma, n) = [for(i=[comma[n]+1:comma[n+1]-1]) a[i]];
function vec2num(l, I=undef) = let(i=I==undef?len(l)-1:I)
let(digit = search(l[i], nums, 0)[0][0])
i>0 ? 10*vec2num(l, i-1)+digit: digit;
--
Sent from: http://forum.openscad.org/
Thx. Very nice code. I usually faint right away when seeing "search" in the
code, but this one deserves a deeper look. :)
Parkinbot wrote
This is not so trivial. See the following code.
$ Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText ( OpenSCAD lexer ); $ Tips ; $ Snippets
--
Sent from: http://forum.openscad.org/
yeah, in lack of asc() or a similar bridge function, search() can to be
"abused" for conversion.
OpenSCAD puts strings a bit into second place. Especially a parse() function
as well as substring() would be a nice to have.
--
Sent from: http://forum.openscad.org/