search a solution converting special string to array for customizer

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

search a solution converting special string to array for customizer

Moster
Hello,
I'm rather new using OpenSCAD and now I have a problem to convert a certain type of string to a special array.
it would be nice if someone can help me.
The background: because with OpenSCAD customizer its only possible to edit strings and not arrays, with more than 1Dim,
I try to convert a string form a field in customizer to a group of arrays, so that this can be further handeld by OpenScad.
The result should be a group of arrays like in "ArrTest"
Here is my Example-code:
echo("---- Example conv. string to group of array -----");

// this string has to be converted to group of arrays:
// String can be changed in customizer
String="[1,2,3];[4,5,6];[7,8,9]";

// result, like it must be,but not editable in customizer
ArrTest=[[1,2,3],[4,5,6],[7,8,9]];

echo("ArrTest:",ArrTest);
echo("len(ArrTest): ",len(ArrTest));

ArrNew=[for(i=[0:len(ArrTest)-1]) ArrTest[i]];
echo("ArrNew:",ArrNew);
echo("Single Element: ArrNew[0][0]:",ArrNew[0][0]);

// the result of conversion, but not fully parameterized
ArrNewOk=[for(i=[0:len(ArrTest)-1]) [ArrTest[i][0],ArrTest[i][1],ArrTest[i][2]]];
echo("ArrNewOk(but 2nd part w.numbers not dyn.):",ArrNewOk);

// false Tries
ArrTry1=[for(i=[0:len(ArrTest)-1]) for(j=[0:2]) [ArrTest[i][j],ArrTest[i][j],ArrTest[i][j]]];
echo("ArrTry1(wrong):",ArrTry1);

ArrTry2=[for(i=[0:len(ArrTest)-1]) for(j=[0:2]) [ArrTest[i][j]]] ;
echo("ArrTry2(wrong):",ArrTry2);


"String": this string that can be changed in customizer
"ArrTest": example-string of the resultingmust be converted in this form
with help of some ext.libs (strings and TOUL),  I have converted the input-string to the form in "ArrNewOk".
I can select each array and each element of the arrays. Thats ok so far.
This form has the desired result, but the second element of the array has numbers and not vars.
I cannot find a way to build up the second loop (without using brackets in the for loop), so that the result is correct.
"ArrTry1" and "ArrtTry2" give all incorrect results.

Has anyone a hint to solve my problem?
Miguel

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

MichaelAtOz
Administrator
Hang in there Moster, I'm almost there.
I went the hard way, but just had a thought of a simpler way, depending.

Are you too fussed on internals?
Given it is three groups, separated by ';' of 3 numbers, separated by ',', and various '[]'.
Are you OK with ',' instead of ';' (ie "[1,2,3],[4,5,6],[7,8,9]")?
I can just extracted 9 numbers, then return them as a vector of three vectors as you specified is that ok?
I would ignore any '[' & ']' (thus having 9 numbers separated by ',')
Just makes it a bit simpler & concise code.

Either way, as I'm close on the original '...];[...' solution.

Noting that there is no syntax checking.
OpenSCAD Admin - email* me if you need anything, or if I've done something stupid...
* on the Forum, click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain;
to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
OpenSCAD Admin - email* me if you need anything, or if I've done something stupid...
* on the Forum, click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain;
to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

Moster
Hi Michael,
thx for your answer.
yes, a possible input in form of [1,2,3],[1,2,3].. would be optimal. I had choosen the separator ";" only because customizer isn't capable to handle it as input. I need only tese group of arrays.
 Meanwhile I have worked at a solution, what fits, but not nice and rather complex, a nightmare to program for me, because at the end it has to be a function. I used some code from your examples, Thanks a lot, and the lib "strings.scad" from "16807" published on thingiverse(https://www.thingiverse.com/thing:526023). I had choosen the easier way and not to reinvent the wheel.
Here my result-code:

include <strings.scad>
// string as input customizer
string="[1,2,3,3];[4,5,6,10]";
// the result is a coorect formatted group of array
aNew=str2arr(string);
echo(aNew);


//*****************************************************+
// function str2arr (string)
// concetanation from these code lines:
// remove brackets
/*
sShort=replace((replace(sFP_Holes, "[", "")), "]", "");
// number of Elements of the pseudo-StringArray, its format is string, ";" as separator
aNumSep = split(sShort,";"); // result are 3 arrays with strings as element
// Elements of inner array als string
aNumSepInner = [for(i=[0:len(aNumSep)-1]) (split(aNumSep[i],","))];
// inner array, Element[0][0] as len-value, all Elements same dim.
arr = [for (i=[0:len(aNumSep)-1]) for(j=[0:len(aNumSepInner[0])-1]) (str2num(aNumSepInner[i][j]))];
// seperate array in Groups
aNew = array_group(arr,len(aNumSepInner[0]));
*/
function str2arr(string)=
 array_group([for (i=[0:len(split((replace((replace(string, "[", "")), "]", "")),";"))-1]) for(j=[0:len(([for(i=[0:len((split((replace((replace(string, "[", "")), "]", "")),";")))-1]) (split((split((replace((replace(string, "[", "")), "]", "")),";"))[i],","))])[0])-1]) (str2num(([for(i=[0:len((split((replace((replace(string, "[", "")), "]", "")),";")))-1]) (split((split((replace((replace(string, "[", "")), "]", "")),";"))[i],","))])[i][j]))],len([for(i=[0:len((split((replace((replace(string, "[", "")), "]", "")),";")))-1]) (split((split((replace((replace(string, "[", "")), "]", "")),";"))[i],","))][0]));

//********************************************************

function array_group(v, cnt=2, dflt=0) =
    [for (i = [0:cnt:len(v)-1]) [for (j = [0:1:cnt-1]) default(v[i+j], dflt)]];

// Function: default()
// Usage:
//   val = default(val, dflt);
// Topics: Undef Handling
// See Also: first_defined(), one_defined(), num_defined()
// Description:
//   Returns the value given as `v` if it is not `undef`.
//   Otherwise, returns the value of `dflt`.
// Arguments:
//   v = Value to pass through if not `undef`.
//   dflt = Value to return if `v` *is* `undef`.
function default(v,dflt=undef) = is_undef(v)? dflt : v;
   
// released to the public domain as "hex.scad", by MichaelAtOz
function str2num(h="0",i=-1) =
// convers a string of digits into a decimal number
    i == -1
    ? str2num(h,i=len(h)-1)
    :   i == 0
        ? decDigit(h[0])
        : decDigit(h[i]) + 10*str2num(h,i-1);

function decDigit(d="") =
    len(d) != 1 ? undef :
    d[0] == "0" ? 0 :
    d[0] == "1" ? 1 :
    d[0] == "2" ? 2 :
    d[0] == "3" ? 3 :
    d[0] == "4" ? 4 :
    d[0] == "5" ? 5 :
    d[0] == "6" ? 6 :
    d[0] == "7" ? 7 :
    d[0] == "8" ? 8 :
    d[0] == "9" ? 9 : (0/0); // 0/0=nan

//--------------------------------------



thanks and regards Miguel

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

GeoffHorton
function decDigit(d="") =
len(d) != 1? undef : ord(d[0]) - ord("0"));

Not as clear, perhaps, but shorter.

On Sun, May 23, 2021 at 8:29 AM Moster <[hidden email]> wrote:
Hi Michael,
thx for your answer.
yes, a possible input in form of [1,2,3],[1,2,3].. would be optimal. I had choosen the separator ";" only because customizer isn't capable to handle it as input. I need only tese group of arrays.
 Meanwhile I have worked at a solution, what fits, but not nice and rather complex, a nightmare to program for me, because at the end it has to be a function. I used some code from your examples, Thanks a lot, and the lib "strings.scad" from "16807" published on thingiverse(https://www.thingiverse.com/thing:526023). I had choosen the easier way and not to reinvent the wheel.
Here my result-code:

include <strings.scad>
// string as input customizer
string="[1,2,3,3];[4,5,6,10]";
// the result is a coorect formatted group of array
aNew=str2arr(string);
echo(aNew);


//*****************************************************+
// function str2arr (string)
// concetanation from these code lines:
// remove brackets
/*
sShort=replace((replace(sFP_Holes, "[", "")), "]", "");
// number of Elements of the pseudo-StringArray, its format is string, ";" as separator
aNumSep = split(sShort,";"); // result are 3 arrays with strings as element
// Elements of inner array als string
aNumSepInner = [for(i=[0:len(aNumSep)-1]) (split(aNumSep[i],","))];
// inner array, Element[0][0] as len-value, all Elements same dim.
arr = [for (i=[0:len(aNumSep)-1]) for(j=[0:len(aNumSepInner[0])-1]) (str2num(aNumSepInner[i][j]))];
// seperate array in Groups
aNew = array_group(arr,len(aNumSepInner[0]));
*/
function str2arr(string)=
 array_group([for (i=[0:len(split((replace((replace(string, "[", "")), "]", "")),";"))-1]) for(j=[0:len(([for(i=[0:len((split((replace((replace(string, "[", "")), "]", "")),";")))-1]) (split((split((replace((replace(string, "[", "")), "]", "")),";"))[i],","))])[0])-1]) (str2num(([for(i=[0:len((split((replace((replace(string, "[", "")), "]", "")),";")))-1]) (split((split((replace((replace(string, "[", "")), "]", "")),";"))[i],","))])[i][j]))],len([for(i=[0:len((split((replace((replace(string, "[", "")), "]", "")),";")))-1]) (split((split((replace((replace(string, "[", "")), "]", "")),";"))[i],","))][0]));

//********************************************************

function array_group(v, cnt=2, dflt=0) =
    [for (i = [0:cnt:len(v)-1]) [for (j = [0:1:cnt-1]) default(v[i+j], dflt)]];

// Function: default()
// Usage:
//   val = default(val, dflt);
// Topics: Undef Handling
// See Also: first_defined(), one_defined(), num_defined()
// Description:
//   Returns the value given as `v` if it is not `undef`.
//   Otherwise, returns the value of `dflt`.
// Arguments:
//   v = Value to pass through if not `undef`.
//   dflt = Value to return if `v` *is* `undef`.
function default(v,dflt=undef) = is_undef(v)? dflt : v;
   
// released to the public domain as "hex.scad", by MichaelAtOz
function str2num(h="0",i=-1) =
// convers a string of digits into a decimal number
    i == -1
    ? str2num(h,i=len(h)-1)
    :   i == 0
        ? decDigit(h[0])
        : decDigit(h[i]) + 10*str2num(h,i-1);

function decDigit(d="") =
    len(d) != 1 ? undef :
    d[0] == "0" ? 0 :
    d[0] == "1" ? 1 :
    d[0] == "2" ? 2 :
    d[0] == "3" ? 3 :
    d[0] == "4" ? 4 :
    d[0] == "5" ? 5 :
    d[0] == "6" ? 6 :
    d[0] == "7" ? 7 :
    d[0] == "8" ? 8 :
    d[0] == "9" ? 9 : (0/0); // 0/0=nan

//--------------------------------------



thanks and regards Miguel

Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

Moster
This post was updated on .
I have tried your hint, but cannot get the desired result?.
In easy words what the code should do, so it can be changed by customizer:
input: string="[1,2,3];[4,5,6]..."
output: array=[[1, 2, 3],[4, 5, 6]...]



--
Sent from: http://forum.openscad.org/
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

MichaelAtOz
Administrator

Sorry, that is in regards to me coding a function to convert the string into the vector format you want.

It doesn't happen automatically.

With ',' instead of ';' it is just easier to code it.

 


From: Moster [mailto:[hidden email]]
Sent: Mon, 24 May 2021 03:48
To: [hidden email]
Subject: [OpenSCAD] Re: search a solution converting special string to array for customizer

 

I have tried your hint, but cannot get the desired result?.
In easy words what the code should do, so it can be changed by customizer:
input: string="[1,2,3];[4,5,6];...
output: array=[[1, 2, 3 ], [4, 5, 6]...]


Sent from the OpenSCAD mailing list archive at Nabble.com.


Virus-free. www.avg.com

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
OpenSCAD Admin - email* me if you need anything, or if I've done something stupid...
* on the Forum, click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain;
to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

MichaelAtOz
Administrator
In reply to this post by GeoffHorton
GeoffHorton wrote
function decDigit(d="") =
len(d) != 1? undef : ord(d[0]) - ord("0"));

Not as clear, perhaps, but shorter.
Luxury!, when I wrote that I could only dream of built in functions... ord(), we had to hand set the octal inputs on the main console...

Moster wrote
Meanwhile I have worked at a solution, what fits, but not nice and rather complex, a nightmare
Nightmare? When I saw your str2arr() I nearly choked ;)
And it takes 8 seconds!
OK it works, so no hurry, but I'll try my way.
So, to clarify, originally I thought you wanted three groups of three, now it looks like you want a generic solution for any mix of nested vectors??
OpenSCAD Admin - email* me if you need anything, or if I've done something stupid...
* on the Forum, click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain;
to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
OpenSCAD Admin - email* me if you need anything, or if I've done something stupid...
* on the Forum, click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain;
to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

Moster
yes, the resulting function ist horrible. It takes so a long time because of the high nested operations. As multiple code-lines its quick and easier to understand, but if possible I need a function. I need this convertion in multiple parts of the program.
I dont know how to build a function with multiple commands in OpenScad, so I had clipped the commands together to a single one, to get a function. not fine, I know.

the function should handle array group with same dimension: [1,2,3],[4,5,6]... so that these can be expand or changed in their contents (not in dimension). f.e. to chage it to [1,2,3],[10,12,23] in the customizer.

The "golden" solution would be that it can also handle strings [1,2,"text1"],[4,5,"text2"]..
I worked at this code actually, but its also this rather unmanageable solution.

the aim of this is to expand and fully parametrize a program for creating enclosures for electr. parts that was published at thingiverse: The Ultimate Box Maker https://www.thingiverse.com/thing:1264391


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

GeoffHorton
What you're up against is that OpenSCAD doesn't have very strong string support. It's not often needed, so I see why it's not high on anyone's priority list, but it means string operations will be slow and clunky.

For what you're doing, I'd be strongly tempted into trying a macro processor (M4 or something less quaint, depending on your preferences), or write something in Python. OpenSCAD is Turing-complete (I think), but that doesn't make it the right tool for everything.

On Mon, May 24, 2021 at 6:12 AM Moster <[hidden email]> wrote:
yes, the resulting function ist horrible. It takes so a long time because of the high nested operations. As multiple code-lines its quick and easier to understand, but if possible I need a function. I need this convertion in multiple parts of the program.
I dont know how to build a function with multiple commands in OpenScad, so I had clipped the commands together to a single one, to get a function. not fine, I know.

the function should handle array group with same dimension: [1,2,3],[4,5,6]... so that these can be expand or changed in their contents (not in dimension). f.e. to chage it to [1,2,3],[10,12,23] in the customizer.

The "golden" solution would be that it can also handle strings [1,2,"text1"],[4,5,"text2"]..
I worked at this code actually, but its also this rather unmanageable solution.

the aim of this is to expand and fully parametrize a program for creating enclosures for electr. parts that was published at thingiverse: The Ultimate Box Maker https://www.thingiverse.com/thing:1264391


Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

MichaelAtOz
Administrator
GeoffHorton wrote
OpenSCAD is Turing-complete (I think), but that
doesn't make it the right tool for everything.
Yep Turing-complete after concat() was added some years ago.
OpenSCAD Admin - email* me if you need anything, or if I've done something stupid...
* on the Forum, click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain;
to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
OpenSCAD Admin - email* me if you need anything, or if I've done something stupid...
* on the Forum, click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain;
to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

50an6xy06r6n
So I've been running into the same issue where the customizer can't handle nested arrays, but why can't support for that just be added natively instead of trying to come up with weird string parsing workarounds? Like at this point it seems much easier to write a script to generate a .scad header file directly than try to mess around with the customizer json.

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: search a solution converting special string to array for customizer

50an6xy06r6n
Raised as Issue #3817

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]