Displaying integers in a text object?

Discuss Lemur and share techniques.
oldgearguy
Regular
Posts: 315
Joined: 02 Nov 2013 11:19

Re: Displaying integers in a text object?

Post by oldgearguy »

GV1 wrote:Testing now. I'm not sure how I can make it more elegant except to contain it in a function ... that's a solid solution to be honest.
Thanks.
Definitely wrap it in a function. What got me was having to do the various gymnastics on i. From a programmer's POV, doing an i-- and then the "- 1" in the val[] offset seemed a bit crude, but it saved me from using a pair of braces and separately decrementing i (maybe). I was also thinking about a reverse-in-place on the array, but I was getting tired and wanted to post something that could be used (and understood and modified if necessary).

Just one of those things -- programming allows hundreds of different solutions, but it's always nice to find an elegant one if possible.
GV1
Newbie
Posts: 38
Joined: 03 Jan 2012 19:33

Re: Displaying integers in a text object?

Post by GV1 »

Actually it works for single numbers, but when I use the number 869 it will only print 86 and ignore the 9. I'll debug it and get it working :)
GV1
Newbie
Posts: 38
Joined: 03 Jan 2012 19:33

Re: Displaying integers in a text object?

Post by GV1 »

The following works fine:

Code: Select all

/*
 * This function converts an integer value to a string.
 * intVal: integer value
 */
decl val, tmp, j, i=0;

if(intVal == 0) return '0';

while(intVal>0) {
	tmp[i++] = (intVal % 10) + 0x30;
	intVal = floor(intVal / 10);
}

for(j=0; j<=sizeof(tmp); j++)
	val[j] = tmp[sizeof(tmp) - j - 1];

return arraytostring(val);
Last edited by GV1 on 19 Oct 2016 12:22, edited 1 time in total.
oldgearguy
Regular
Posts: 315
Joined: 02 Nov 2013 11:19

Re: Displaying integers in a text object?

Post by oldgearguy »

GV1 wrote:Actually it works for single numbers, but when I use the number 869 it will only print 86 and ignore the 9. I'll debug it and get it working :)
Hmm - the original worked for me across a wide range of numbers. Maybe in moving it to a function something got missed.

Anyway, your posted solution is nice and clean. The reason brevity is sometimes necessary is that there is a hard limit to the size of the .jzml files. Once you get a large application going, you'll be looking for ways to save space. Removing comments is an obvious first step, but even then it may not be enough.

For small templates, it's not an issue of course, but I have continually run into that limit when building the few editors/apps I've created.
GV1
Newbie
Posts: 38
Joined: 03 Jan 2012 19:33

Re: Displaying integers in a text object?

Post by GV1 »

oldgearguy wrote:
GV1 wrote:Actually it works for single numbers, but when I use the number 869 it will only print 86 and ignore the 9. I'll debug it and get it working :)
Hmm - the original worked for me across a wide range of numbers. Maybe in moving it to a function something got missed.

Anyway, your posted solution is nice and clean. The reason brevity is sometimes necessary is that there is a hard limit to the size of the .jzml files. Once you get a large application going, you'll be looking for ways to save space. Removing comments is an obvious first step, but even then it may not be enough.

For small templates, it's not an issue of course, but I have continually run into that limit when building the few editors/apps I've created.
Ah yeah I forgot the Lemur is limited in memory. I've done a fair but of code golfing in the past but I don't know enough about Lemur scripting to be able to do that. Would be cool to create a transpiler eventually.
Post Reply