discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

generalized zero fill on numbers?

JD
Jerry Davis
Wed, Jan 4, 2017 3:12 PM

When I use the version_num(), I get a exponent type number.
Not at all what I expected, but that's ok. I figured out another way to get
what I really want, but it is strange.

Consider the following code:

function fzs2(x) = len(str(x)) < 2 ? str("0",x) : str(x);
//function fzs3(x) = len(str(x)) < 3 ? str("00",x) : str(x);
//function fzs4(x) = len(str(x)) < 4 ? str("000",x) : str(x);
//function fzs5(x) = len(str(x)) < 5 ? str("0000",x) : str(x);

echo(version());    // [2016,11,6]
echo(str(version()[0],
fzs2(version()[1]),
fzs2(version()[2])));    // "20161106" which is what I want.

I thought, ok: this works in this special case.

Then I got to thinking about the printf statement in C for instance.
Where "%.4d" would print out any integer with leading zero's filled in to
the width of the number.

So, I tried defining the following functions, and of course they only work
for special cases.

Like what if I do fzs3(12) for instance? it comes back "0012" and not "012"
like in the printf case. But of course, fzs3(3) would be fine.

So, is there any way to do this generally? without being really messy?

Just wondering.

Regards,
Jerry

--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Openscad developer

The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

When I use the version_num(), I get a exponent type number. Not at all what I expected, but that's ok. I figured out another way to get what I really want, but it is strange. Consider the following code: function fzs2(x) = len(str(x)) < 2 ? str("0",x) : str(x); //function fzs3(x) = len(str(x)) < 3 ? str("00",x) : str(x); //function fzs4(x) = len(str(x)) < 4 ? str("000",x) : str(x); //function fzs5(x) = len(str(x)) < 5 ? str("0000",x) : str(x); echo(version()); // [2016,11,6] echo(str(version()[0], fzs2(version()[1]), fzs2(version()[2]))); // "20161106" which is what I want. I thought, ok: this works in this special case. Then I got to thinking about the printf statement in C for instance. Where "%.4d" would print out any integer with leading zero's filled in to the width of the number. So, I tried defining the following functions, and of course they only work for special cases. Like what if I do fzs3(12) for instance? it comes back "0012" and not "012" like in the printf case. But of course, fzs3(3) would be fine. So, is there any way to do this generally? without being really messy? Just wondering. Regards, Jerry -- Extra Ham Operator: K7AZJ Registered Linux User: 275424 Raspberry Pi and Openscad developer *The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".*- Isaac. Asimov
P
Parkinbot
Wed, Jan 4, 2017 3:32 PM

Try a recursive version of your function.

v = version();
n = 2;
echo(v);    // [2016,11,6]
echo(str(
fzs(4, v[0]),
fzs(n, v[1]),
fzs(n, v[2])
));    // "20161106" which is what I want.

function fzs(n=1, x) = len(str(x)) < n ? fzs(n, str("0",x)) : str(x);

--
View this message in context: http://forum.openscad.org/generalized-zero-fill-on-numbers-tp19916p19918.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Try a recursive version of your function. > v = version(); > n = 2; > echo(v); // [2016,11,6] > echo(str( > fzs(4, v[0]), > fzs(n, v[1]), > fzs(n, v[2]) > )); // "20161106" which is what I want. > > function fzs(n=1, x) = len(str(x)) < n ? fzs(n, str("0",x)) : str(x); -- View this message in context: http://forum.openscad.org/generalized-zero-fill-on-numbers-tp19916p19918.html Sent from the OpenSCAD mailing list archive at Nabble.com.
A
adrian
Mon, Jan 9, 2017 3:08 AM

Parkinbot wrote

Try a recursive version of your function.

v = version();
n = 2;
echo(v);    // [2016,11,6]
echo(str(
fzs(4, v[0]),
fzs(n, v[1]),
fzs(n, v[2])
));    // "20161106" which is what I want.

function fzs(n=1, x) = len(str(x)) < n ? fzs(n, str("0",x)) : str(x);

Technically, you could do that, but if you wanted to do it slightly more
efficiently you would use tail recursion:

It doesn't really matter in this case as x is likely to be small, but it is
just a good habit to get into.  See  Recursive Functions
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Recursive_functions
for more information.

--
View this message in context: http://forum.openscad.org/generalized-zero-fill-on-numbers-tp19916p19959.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Parkinbot wrote > Try a recursive version of your function. >> v = version(); >> n = 2; >> echo(v); // [2016,11,6] >> echo(str( >> fzs(4, v[0]), >> fzs(n, v[1]), >> fzs(n, v[2]) >> )); // "20161106" which is what I want. >> >> function fzs(n=1, x) = len(str(x)) < n ? fzs(n, str("0",x)) : str(x); Technically, you could do that, but if you wanted to do it slightly more efficiently you would use tail recursion: It doesn't really matter in this case as x is likely to be small, but it is just a good habit to get into. See Recursive Functions <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Recursive_functions> for more information. -- View this message in context: http://forum.openscad.org/generalized-zero-fill-on-numbers-tp19916p19959.html Sent from the OpenSCAD mailing list archive at Nabble.com.
R
Ronaldo
Mon, Jan 9, 2017 11:44 AM

adrian wrote

Parkinbot wrote

Try a recursive version of your function.

v = version();
n = 2;
echo(v);    // [2016,11,6]
echo(str(
fzs(4, v[0]),
fzs(n, v[1]),
fzs(n, v[2])
));    // "20161106" which is what I want.

function fzs(n=1, x) = len(str(x)) < n ? fzs(n, str("0",x)) : str(x);

Technically, you could do that, but if you wanted to do it slightly more
efficiently you would use tail recursion:

It doesn't really matter in this case as x is likely to be small, but it
is just a good habit to get into.  See
Recursive Functions
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Recursive_functions
for more information.

I usually prefer the form you suggest but both of them are eligible to tail
recursion elimination as noted in the reference you mentioned.

--
View this message in context: http://forum.openscad.org/generalized-zero-fill-on-numbers-tp19916p19966.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

adrian wrote > > Parkinbot wrote >> Try a recursive version of your function. >>> v = version(); >>> n = 2; >>> echo(v); // [2016,11,6] >>> echo(str( >>> fzs(4, v[0]), >>> fzs(n, v[1]), >>> fzs(n, v[2]) >>> )); // "20161106" which is what I want. >>> >>> function fzs(n=1, x) = len(str(x)) < n ? fzs(n, str("0",x)) : str(x); > Technically, you could do that, but if you wanted to do it slightly more > efficiently you would use tail recursion: > > It doesn't really matter in this case as x is likely to be small, but it > is just a good habit to get into. See > Recursive Functions > <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Recursive_functions> > for more information. I usually prefer the form you suggest but both of them are eligible to tail recursion elimination as noted in the reference you mentioned. -- View this message in context: http://forum.openscad.org/generalized-zero-fill-on-numbers-tp19916p19966.html Sent from the OpenSCAD mailing list archive at Nabble.com.
A
adrian
Mon, Jan 9, 2017 1:47 PM

Ronaldo wrote

I usually prefer the form you suggest but both of them are eligible to
tail recursion elimination as noted in the reference you mentioned.

Huh.  Strange.  I must have been a zombie when I posted that. :)

--
View this message in context: http://forum.openscad.org/generalized-zero-fill-on-numbers-tp19916p19975.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Ronaldo wrote > I usually prefer the form you suggest but both of them are eligible to > tail recursion elimination as noted in the reference you mentioned. Huh. Strange. I must have been a zombie when I posted that. :) -- View this message in context: http://forum.openscad.org/generalized-zero-fill-on-numbers-tp19916p19975.html Sent from the OpenSCAD mailing list archive at Nabble.com.