Hi, this might be a stupid question, but looking through the documentation I
couldn't find the answer.
Does OpenSCAD have a way of defining a function which has a returnvalue?
I'd like to do something like this:
function multiply(a, b) {
return a*b;
}
or
function substring(string, start, length) {
substring = "";
for (i = [start : start+length])
substring = string[i];
return substring;
}
I was actually looking for a substring() method so that I could grab parts
of a string, and then that turned into a hunt for making my own function for
it, but I can't see any way to create functions.
--
View this message in context: http://forum.openscad.org/Functions-with-returnvalue-tp10842.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
All functions have a return value but they are a single expression, not a
procedural body so no return statement.
function multiply(a, b) = a * b;
For iteration use recursion or list comprehension.
function function substring(string, start, length) = start < length ?
concat(string[start], substring(string, start + 1, length) : "";
I think, not tested.
On 31 December 2014 at 22:36, Scalpel78 frode@lillerud.no wrote:
Hi, this might be a stupid question, but looking through the documentation
I
couldn't find the answer.
Does OpenSCAD have a way of defining a function which has a returnvalue?
I'd like to do something like this:
function multiply(a, b) {
return a*b;
}
or
function substring(string, start, length) {
substring = "";
for (i = [start : start+length])
substring = string[i];
return substring;
}
I was actually looking for a substring() method so that I could grab parts
of a string, and then that turned into a hunt for making my own function
for
it, but I can't see any way to create functions.
--
View this message in context:
http://forum.openscad.org/Functions-with-returnvalue-tp10842.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
This one works:
function substring(string, start, length) = start < length ?
str(string[start], substring(string, start + 1, length)) : "";
echo(substring("abcdef", 2,4));
ECHO: "cd"
On 31 December 2014 at 23:24, nop head nop.head@gmail.com wrote:
All functions have a return value but they are a single expression, not a
procedural body so no return statement.
function multiply(a, b) = a * b;
For iteration use recursion or list comprehension.
function function substring(string, start, length) = start < length ?
concat(string[start], substring(string, start + 1, length) : "";
I think, not tested.
On 31 December 2014 at 22:36, Scalpel78 frode@lillerud.no wrote:
Hi, this might be a stupid question, but looking through the
documentation I
couldn't find the answer.
Does OpenSCAD have a way of defining a function which has a returnvalue?
I'd like to do something like this:
function multiply(a, b) {
return a*b;
}
or
function substring(string, start, length) {
substring = "";
for (i = [start : start+length])
substring = string[i];
return substring;
}
I was actually looking for a substring() method so that I could grab parts
of a string, and then that turned into a hunt for making my own function
for
it, but I can't see any way to create functions.
--
View this message in context:
http://forum.openscad.org/Functions-with-returnvalue-tp10842.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
function substring(start, count, v) = [for (i=[start:start+count-1]) v[i]];
echo(substring(4, 3, [0,1,2,3,4,5,6,7,8,9,10]));
echo(substring(4, 3,"012345678910"));
You may want to modify it to [start-1:start+count-2] if you want the first
index to be 1.
--
View this message in context: http://forum.openscad.org/Functions-with-returnvalue-tp10842p10861.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Thanks for the suggestions, guys. It's almost there, but not quite.
nopheads variant takes in start and end, but I want start and length.
Bananapeels variant returns a list(?) of characters, not a string.
I tried to write my own, like this, but it fails on a syntax error, and I
don't see why:
function substring(string, start, length) = for(i=[start:start+length-1])
str(string[i]);
--
View this message in context: http://forum.openscad.org/Functions-with-returnvalue-tp10842p10862.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Sorry, this is probably what you want:
function substring(string, start, length) = length > 0 ? str(string[start],
substring(string, start + 1, length - 1)) : "";
echo(substring("abcdef", 2,3));
ECHO: "cde"
Your version is a syntax error because you have used a for statement where
an expressions is required. Possibly confused by list comprehension syntax
that can build lists with a for statement that can then be used in an
expression where a list is needed.
On 1 January 2015 at 17:45, Scalpel78 frode@lillerud.no wrote:
Thanks for the suggestions, guys. It's almost there, but not quite.
nopheads variant takes in start and end, but I want start and length.
Bananapeels variant returns a list(?) of characters, not a string.
I tried to write my own, like this, but it fails on a syntax error, and I
don't see why:
function substring(string, start, length) = for(i=[start:start+length-1])
str(string[i]);
--
View this message in context:
http://forum.openscad.org/Functions-with-returnvalue-tp10842p10862.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
Scalpel78 wrote
Bananapeels variant returns a list(?) of characters, not a string.
You are right, but why does it matter?
--
View this message in context: http://forum.openscad.org/Functions-with-returnvalue-tp10842p10875.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
/* Convert all elements of a vector to string and concatenate*/
function toStr(v, i=0) = i<len(v) ? str(v[i], toStr(v, i+1)) : "";
/* Return a range of elements from a vector or string as a vector */
function subSection(start, count, v) = [for (i=[start:start+count-1]) v[i]];
/* Return a range of elements from a vector or string as a string */
function subStr(start, count, v) = toStr(subSection(start, count, v));
echo(subStr(4, 3, [0,1,2,3,4,5,6,7,8,9,10]));
echo(subStr(4, 3,"012345678910"));
--
View this message in context: http://forum.openscad.org/Functions-with-returnvalue-tp10842p10876.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Thanks Bananapeel! I'm not good enough with the functions, lists, expressions
etc to write those myself, so thanks for making them :)
--
View this message in context: http://forum.openscad.org/Functions-with-returnvalue-tp10842p10882.html
Sent from the OpenSCAD mailing list archive at Nabble.com.