discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

"Ignoring unknown variable" issue...

N
Neon22
Wed, Jul 15, 2015 1:42 PM

wow that's really clever. But maybe in openSCAD2 we should try to make it
siompler like have dictionaries - e.g. Python dictionaries, or LISP like
association lists.
E.g. stuff = {"name1" : 10, "name2" : 20, "name3" : [12,22]}
and stuff["name1"] returns 10.

The loss of assign has helped to make the code more readable.
and we have mandatory and optional and keyword arguments to modules which is
great.

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13168.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

wow that's really clever. But maybe in openSCAD2 we should try to make it siompler like have dictionaries - e.g. Python dictionaries, or LISP like association lists. E.g. stuff = {"name1" : 10, "name2" : 20, "name3" : [12,22]} and stuff["name1"] returns 10. The loss of assign has helped to make the code more readable. and we have mandatory and optional and keyword arguments to modules which is great. -- View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13168.html Sent from the OpenSCAD mailing list archive at Nabble.com.
A
Amedee
Wed, Jul 15, 2015 4:10 PM

Actually with the latest version of OpenSCAD we have a couple of interesting
features which make possible to emulate associative arrays (or dictionaries)
easily...

A simple example script:

// Associative arrays...

default_values = [
["a", 10],
["b", 20],
["bb", 100]
];

parameters = [
["bb", 200]
];

// Get value from associative array
function ar_get(ar, key) =
let(idx = search([key],ar)[0])
ar[idx][1];

// Merge 2 arrays (values from ar2 overrides ar1)
function ar_merge(ar1, ar2) = [ for (elem = ar1)
let(idx = search([elem[0]],ar2)[0])
[elem[0], idx != [] ? ar2[idx][1] : elem[1]]
];

// Example:
echo ("Default bb is:", ar_get(default_values, "bb"));

merged_values = ar_merge(default_values, parameters);
echo ("Actual bb is:", ar_get(merged_values, "bb"));

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13173.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Actually with the latest version of OpenSCAD we have a couple of interesting features which make possible to emulate associative arrays (or dictionaries) easily... A simple example script: // Associative arrays... default_values = [ ["a", 10], ["b", 20], ["bb", 100] ]; parameters = [ ["bb", 200] ]; // Get value from associative array function ar_get(ar, key) = let(idx = search([key],ar)[0]) ar[idx][1]; // Merge 2 arrays (values from ar2 overrides ar1) function ar_merge(ar1, ar2) = [ for (elem = ar1) let(idx = search([elem[0]],ar2)[0]) [elem[0], idx != [] ? ar2[idx][1] : elem[1]] ]; // Example: echo ("Default bb is:", ar_get(default_values, "bb")); merged_values = ar_merge(default_values, parameters); echo ("Actual bb is:", ar_get(merged_values, "bb")); -- View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13173.html Sent from the OpenSCAD mailing list archive at Nabble.com.
R
runsun
Wed, Jul 15, 2015 6:44 PM

Amedee wrote

Actually with the latest version of OpenSCAD we have a couple of
interesting features which make possible to emulate associative arrays (or
dictionaries) easily...

A simple example script:

// Associative arrays...

default_values = [
["a", 10],
["b", 20],
["bb", 100]
];

Indeed. We had a discussion about this  here
http://forum.openscad.org/Digging-into-search-td12421.html  . it was when
the change made.

However, it requires array of [key,val]s,  means to type in lots of [ ],
which is I try to avoid. So in my hash parameter model, I use a flat array
like [  "a", 10,  "b", 20,  "bb", 100  ].

In fact, it doesn't need latest version of OpenSCAD to come up with several
user function for array of [key,val]'s to work like an associate array.

But it's just my personal taste. Your code does solve the problem and would
probably run faster than mine.

Btw, if your keys are all numerical, you can simply use lookup:

function get_cylinder_h(p) = lookup(p, [
[ -200, 5 ],
[ -50, 20 ],
[ -20, 18 ],
[ +80, 25 ],
[ +150, 2 ]
]);

for (i = [-100:5:+100]) {
// echo(i, get_cylinder_h(i));
translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h =
get_cylinder_h(i)*3);
}


$  Runsun Pan, PhD

$ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner

$ -- hash parameter model: here , here

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13175.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Amedee wrote > Actually with the latest version of OpenSCAD we have a couple of > interesting features which make possible to emulate associative arrays (or > dictionaries) easily... > > A simple example script: > > // Associative arrays... > > default_values = [ > ["a", 10], > ["b", 20], > ["bb", 100] > ]; Indeed. We had a discussion about this here <http://forum.openscad.org/Digging-into-search-td12421.html> . it was when the change made. However, it requires array of [key,val]s, means to type in lots of [ ], which is I try to avoid. So in my hash parameter model, I use a flat array like [ "a", 10, "b", 20, "bb", 100 ]. In fact, it doesn't need latest version of OpenSCAD to come up with several user function for array of [key,val]'s to work like an associate array. But it's just my personal taste. Your code does solve the problem and would probably run faster than mine. Btw, if your keys are all numerical, you can simply use lookup: > function get_cylinder_h(p) = lookup(p, [ > [ -200, 5 ], > [ -50, 20 ], > [ -20, 18 ], > [ +80, 25 ], > [ +150, 2 ] > ]); > > for (i = [-100:5:+100]) { > // echo(i, get_cylinder_h(i)); > translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h = > get_cylinder_h(i)*3); > } ----- $ Runsun Pan, PhD $ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner $ -- hash parameter model: here , here $ -- Linux Mint 17.1 Rebecca x64 + OpenSCAD 2015.03.15/2015.04.01.nightly -- View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13175.html Sent from the OpenSCAD mailing list archive at Nabble.com.
MS
Mark Schafer
Wed, Jul 15, 2015 10:50 PM

when using Search I had all kinds of problems in that these two will match:
"grip", "grip-2" when searching for "grip". I.e same for any
substring that starts the same.

Also lookup does interp if there is no match. I wasn't sure I could
count on it for lookups if my field wasn't in the list - but I didn't
extensively test it.

On 7/16/2015 6:44 AM, runsun wrote:

Amedee wrote

Actually with the latest version of OpenSCAD we have a couple of
interesting features which make possible to emulate associative arrays (or
dictionaries) easily...

A simple example script:

// Associative arrays...

default_values = [
["a", 10],
["b", 20],
["bb", 100]
];

Indeed. We had a discussion about this  here
http://forum.openscad.org/Digging-into-search-td12421.html  . it was when
the change made.

However, it requires array of [key,val]s,  means to type in lots of [ ],
which is I try to avoid. So in my hash parameter model, I use a flat array
like [  "a", 10,  "b", 20,  "bb", 100  ].

In fact, it doesn't need latest version of OpenSCAD to come up with several
user function for array of [key,val]'s to work like an associate array.

But it's just my personal taste. Your code does solve the problem and would
probably run faster than mine.

Btw, if your keys are all numerical, you can simply use lookup:

function get_cylinder_h(p) = lookup(p, [
[ -200, 5 ],
[ -50, 20 ],
[ -20, 18 ],
[ +80, 25 ],
[ +150, 2 ]
]);

for (i = [-100:5:+100]) {
// echo(i, get_cylinder_h(i));
translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h =
get_cylinder_h(i)*3);
}


$  Runsun Pan, PhD

$ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner

$ -- hash parameter model: here , here

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13175.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


No virus found in this message.
Checked by AVG - www.avg.com
Version: 2015.0.6081 / Virus Database: 4392/10233 - Release Date: 07/15/15

when using Search I had all kinds of problems in that these two will match: "grip", "grip-2" when searching for "grip". I.e same for any substring that starts the same. Also lookup does interp if there is no match. I wasn't sure I could count on it for lookups if my field wasn't in the list - but I didn't extensively test it. On 7/16/2015 6:44 AM, runsun wrote: > Amedee wrote >> Actually with the latest version of OpenSCAD we have a couple of >> interesting features which make possible to emulate associative arrays (or >> dictionaries) easily... >> >> A simple example script: >> >> // Associative arrays... >> >> default_values = [ >> ["a", 10], >> ["b", 20], >> ["bb", 100] >> ]; > Indeed. We had a discussion about this here > <http://forum.openscad.org/Digging-into-search-td12421.html> . it was when > the change made. > > However, it requires array of [key,val]s, means to type in lots of [ ], > which is I try to avoid. So in my hash parameter model, I use a flat array > like [ "a", 10, "b", 20, "bb", 100 ]. > > In fact, it doesn't need latest version of OpenSCAD to come up with several > user function for array of [key,val]'s to work like an associate array. > > But it's just my personal taste. Your code does solve the problem and would > probably run faster than mine. > > Btw, if your keys are all numerical, you can simply use lookup: > > >> function get_cylinder_h(p) = lookup(p, [ >> [ -200, 5 ], >> [ -50, 20 ], >> [ -20, 18 ], >> [ +80, 25 ], >> [ +150, 2 ] >> ]); >> >> for (i = [-100:5:+100]) { >> // echo(i, get_cylinder_h(i)); >> translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h = >> get_cylinder_h(i)*3); >> } > > > > > ----- > > $ Runsun Pan, PhD > > $ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner > > $ -- hash parameter model: here , here > > $ -- Linux Mint 17.1 Rebecca x64 + OpenSCAD 2015.03.15/2015.04.01.nightly > > > > > -- > View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13175.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > > ----- > No virus found in this message. > Checked by AVG - www.avg.com > Version: 2015.0.6081 / Virus Database: 4392/10233 - Release Date: 07/15/15 > >
A
Amedee
Thu, Jul 16, 2015 6:37 AM

Neon22 wrote

when using Search I had all kinds of problems in that these two will
match:
"grip", "grip-2" when searching for "grip". I.e same for any
substring that starts the same.

You probably had the 'wrong' syntax...

The documentation wrote

Note: If match_value is a vector of strings, search will look for exact
string matches.

So you need to search for ["grip"], not just "grip"

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13183.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Neon22 wrote > when using Search I had all kinds of problems in that these two will > match: > "grip", "grip-2" when searching for "grip". I.e same for any > substring that starts the same. You probably had the 'wrong' syntax... The documentation wrote > Note: If match_value is a vector of strings, search will look for exact > string matches. So you need to search for ["grip"], not just "grip" -- View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13183.html Sent from the OpenSCAD mailing list archive at Nabble.com.
Q
QuackingPlums
Thu, Jul 16, 2015 8:18 AM

Hmm. If I'm reading that hash table approach correctly then isn't it open to
'undefs' in my code if I mistype the hash key? This seems to apply to both
the hash table definition and anywhere I want to lookup hash values in my
code.

The method I use explicitly defines the properties of my 'object', which
means I get the benefits of the compiler telling me if I've tried to access
a non-existent property:

--- code ---
stuff1 = [10, 2, "red"];
stuff2 = [20, 5, "blue"];

function stuff_length(stuff) = stuff[0];
function stuff_width(stuff) = stuff[1];
function stuff_colour(stuff) = stuff[2];

example(stuff1);
module example(myStuff)
{
echo( stuff_length(myStuff) );
echo( stuff_width(myStuff) );
echo( stuff_color(myStuff) );    <--- fails with a warning in the
compiler window as "stuff_color()" is undefined
}
--- /code ---

The drawback is having to maintain the ordered list but I don't see that
being any more onerous than maintaining the keys for every list with the
hash table approach - the order of the parameters in the list doesn't
actually matter (unless you suffer from tidy code OCD like I do!) as we are
abstracting via "property accessors".

Another benefit to using this approach is that you can have derived
properties that are based on calculations of the explicit ones:

function stuff_area(stuff) = stuff_length(stuff) * stuff_width(stuff);

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13187.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Hmm. If I'm reading that hash table approach correctly then isn't it open to 'undefs' in my code if I mistype the hash key? This seems to apply to both the hash table definition and anywhere I want to lookup hash values in my code. The method I use explicitly defines the properties of my 'object', which means I get the benefits of the compiler telling me if I've tried to access a non-existent property: --- code --- stuff1 = [10, 2, "red"]; stuff2 = [20, 5, "blue"]; function stuff_length(stuff) = stuff[0]; function stuff_width(stuff) = stuff[1]; function stuff_colour(stuff) = stuff[2]; example(stuff1); module example(myStuff) { echo( stuff_length(myStuff) ); echo( stuff_width(myStuff) ); echo( stuff_color(myStuff) ); <--- fails with a warning in the compiler window as "stuff_color()" is undefined } --- /code --- The drawback is having to maintain the ordered list but I don't see that being any more onerous than maintaining the keys for every list with the hash table approach - the order of the parameters in the list doesn't actually matter (unless you suffer from tidy code OCD like I do!) as we are abstracting via "property accessors". Another benefit to using this approach is that you can have derived properties that are based on calculations of the explicit ones: function stuff_area(stuff) = stuff_length(stuff) * stuff_width(stuff); -- View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13187.html Sent from the OpenSCAD mailing list archive at Nabble.com.
A
Amedee
Thu, Jul 16, 2015 8:42 AM

I think both methods have also different use cases...

The 'property array' is nice when you have a whole set of objects with
different attributes (like all screw types, bearings, ...)

The hashes are more convenient when you have a base object with (a lot of)
default properties and you want to instantiate it with just a couple of
changes: you don't have to define the complete array, just the amended
key/value.

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13190.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

I think both methods have also different use cases... The 'property array' is nice when you have a whole set of objects with different attributes (like all screw types, bearings, ...) The hashes are more convenient when you have a base object with (a lot of) default properties and you want to instantiate it with just a couple of changes: you don't have to define the complete array, just the amended key/value. -- View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13190.html Sent from the OpenSCAD mailing list archive at Nabble.com.
Q
QuackingPlums
Thu, Jul 16, 2015 10:12 AM

In that scenario I'd use a 'constructor' for my object with default
parameters

function new_stuff(length = 10, width = 2, colour = "yellow")
= [length, width, colour];

myNewPartialObject = new_stuff(width = 15);

I can see how this can start becoming difficult to maintain though if those
definitions change regularly. I do remember reading the hashtable approach
and thinking that it would be useful for certain cases - I just haven't hit
a scenario yet where I have felt the need to refactor.

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13191.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

In that scenario I'd use a 'constructor' for my object with default parameters function new_stuff(length = 10, width = 2, colour = "yellow") = [length, width, colour]; myNewPartialObject = new_stuff(width = 15); I can see how this can start becoming difficult to maintain though if those definitions change regularly. I do remember reading the hashtable approach and thinking that it would be useful for certain cases - I just haven't hit a scenario yet where I have felt the need to refactor. -- View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13191.html Sent from the OpenSCAD mailing list archive at Nabble.com.
N
Neon22
Thu, Jul 16, 2015 11:31 AM

This is all great info. Thanks for it.
I'd like to ask an open question though (because OpenSCAD2 is being
designed).

What do you think woudl be the 'best' way to make this work in OpenSCAD2 ?
Should we add a new accessing method, or type or way of doing this so we
don't need to create special code to do it ?

What aspects of the work arounds are the valuable ones ?

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13193.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

This is all great info. Thanks for it. I'd like to ask an open question though (because OpenSCAD2 is being designed). What do you think woudl be the 'best' way to make this work in OpenSCAD2 ? Should we add a new accessing method, or type or way of doing this so we don't need to create special code to do it ? What aspects of the work arounds are the valuable ones ? -- View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13193.html Sent from the OpenSCAD mailing list archive at Nabble.com.
R
runsun
Thu, Jul 16, 2015 1:36 PM

QuackingPlums wrote

The method I use explicitly defines the properties of my 'object', which
means I get the benefits of the compiler telling me if I've tried to
access a non-existent property:

My current version of the hash looks like this:

It covers the cases of undef:

which can be made to give up a warning:

I have a code to do the string substitution:

So it is possible to make it behave exactly like the compiler warning you
talk about (even more, actually).

The hash would become (note: untested):


$  Runsun Pan, PhD

$ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner

$ -- hash parameter model: here , here

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly

--
View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13197.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

QuackingPlums wrote > The method I use explicitly defines the properties of my 'object', which > means I get the benefits of the compiler telling me if I've tried to > access a non-existent property: My current version of the hash looks like this: It covers the cases of undef: which can be made to give up a warning: I have a code to do the string substitution: So it is possible to make it behave exactly like the compiler warning you talk about (even more, actually). The hash would become (note: untested): ----- $ Runsun Pan, PhD $ -- OpenScad_DocTest ( Thingiverse ), faces , Offliner $ -- hash parameter model: here , here $ -- Linux Mint 17.1 Rebecca x64 + OpenSCAD 2015.03.15/2015.04.01.nightly -- View this message in context: http://forum.openscad.org/Ignoring-unknown-variable-issue-tp13156p13197.html Sent from the OpenSCAD mailing list archive at Nabble.com.