General

Displaying integers in a text object?

Started by GV1 on .

Sign in or create an account to join the conversation.

15 posts

Topic starter
How can I display an integer in a text object?

The following example doesn't work:
decl val = 9;
setattribute(OctaveLabel.octaveValue, 'content', val);
But if I do the following it works fine:
setattribute(OctaveLabel.octaveValue, 'content', '5');
You'll have to concatenate the integer and an empty string and this will turn the value into a string. Like this
decl val = 9;
setattribute(OctaveLabel.octaveValue, 'content', '' + val);
It's not optimal but once you know it it's easy to work around it.

Ah damn, oldgearguy beat me to it
oldgearguy wrote:try this:
decl val = 9;
setattribute(OctaveLabel.octaveValue, 'content', ''+val);

:shock:

outside the box man, outside the box.
I tried that, it didn't work. Really odd. Unless this is only available in newer updates.
GV1 wrote:
oldgearguy wrote:try this:
I tried that, it didn't work. Really odd. Unless this is only available in newer updates.
I forgot you were on original hardware. Does this work?
decl val={0x39};
setattribute (Label, 'content', arraytostring(val));
If it does, that means you'll have to do more math to add 0x30 to each decimal place and do the division to break down the raw number.
Here's a quick and dirty way to take a number and array-ify it. You'll probably want to make it more elegant, but you get the idea:
decl val, tmp;
decl num=4819, i=0, remain;

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

while (i >= 0)
   val[sizeof(tmp) - i - 1] = tmp[i--];

setattribute (Label, 'content', arraytostring(val));
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.
The following works fine:
/*
 * 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);
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.
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.