Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Featurescript: passing by reference.
jrs_spedley
Member Posts: 71 ✭✭
I keep getting stung by passing by reference.
I have a multi dimension array which I am recursively parsing but the only way I can get it to work is if every item is in a box.
e.g as a trivial example ...
I did try returning the array to the same variable in the hope that it would replace it but that doesn't work either ...
Am I missing something or is the only way to do it like the first example?
I have a multi dimension array which I am recursively parsing but the only way I can get it to work is if every item is in a box.
e.g as a trivial example ...
var x=new box([new box(1), new box(2), new box( [new box(3), new box(4)] ) ]);
output(x);
clear(x);
output(x);
function clear(x is box) {
for (var y in x[]) {
if (y[] is array)
clear(y);
else
y[]=0;
}
}
function output(x is box) {
for (var y in x[]) {
if (y[] is array)
output(y);
else
print(y[]);
}
}
I did try returning the array to the same variable in the hope that it would replace it but that doesn't work either ...
var x=[1, 2, [3, 4] ];
x=clear(x);
print(x);
function clear(x) {
for (var y in x) {
if (y is array)
y=clear(y);
else
y=0;
}
return x;
}Am I missing something or is the only way to do it like the first example?
0
Comments
function clear(x) { var xCleared = []; for (var y in x) { if (y is array) xCleared = append(xCleared, clear(y)); else xCleared = append(xCleared, 0);<br> } return xCleared;<br>}var x=[new box(1), new box(2), [new box(3), new box(4)] ]; clear(x); output(x); function clear(x is array) { for (var y in x) { if (y is array) clear(y); else y[]=0; } } function output(x is array) { for (var y in x) { if (y is array) output(y); else print(y[]); } }I use a first pass to calculate minimum sizes and number of parts in each subpart and then a second pass using the dimensions from the first pass to create the parts.
I would have used a different method if I'd have known passing by reference was such a problem when I started!
function clear(x) { for (i=0; i < size(x); i+=1) { if (x[i] is array) x[i] = clear(x[i]); else x[i] = 0;<br> } return x;<br>}I though it was a problem with featurescript as I couldn't see any errors but I'll try again, making a mistake is easy when you try and change code to function differently.