Forums > Javascript

Array output from js object outlet broken?

April 28, 2009 | 11:37 am

I am simply trying to outut a javascript array as a list in Max, but this is not working. Instead of seeing a nice list, I see a list of "jsobject23423423 …" items at the outlet of my object. Yet, the output from post() in the Max window is exactly right – a sequence of numbers. ??????

function makeList()
{
var l = new Array();
for (var i=0; i
l.push(i);

return l;
}

// output list of numbers
function bang()
{
var theList = makeList();
post(theList + "n");

outlet(0, theList);
}



Ch
April 28, 2009 | 11:46 am

You code works nicely.

I just had to add the following lines of course :

inlets = 1;
outlets = 1;
size = 7; // array length

April 28, 2009 | 11:56 am

Sorry, the problem seems to be elsewhere – that was just a piece of my code. I’ve tracked it down properly – it has to do with adding a spliced element to an array. I expect to get an array of numbers (ints) back, but instead I get an array of complex js objects!

code here reproduces the problem:

var size=10;
var theList = new Array();

function loadbang()
{
size=Math.floor(Math.random()*10+2);
post("size: " + size + "n");
theList = new Array();

}

function makeList()
{
var l = new Array();
for (var i=0; i
l.push(i);

return l;
}

// output list of numbers
function bang()
{
theList = makeList();
//post(theList + "n");

var a = new Array();

for (var n=0; n
{
var e = theList.splice(0,1);

// this should add a number!
a.push(e);

// this will print out a number
post(e + "n");
}

// now outputs array of js objects?
outlet(0, a);
}


April 28, 2009 | 11:59 am

A workaround – use parseInt() to make sure that the spliced array element is properly cast to int. But this should not matter, Max should be smart enough to cast the array elements properly – it does it already via the post() function! This is a bug, me thinks.


April 28, 2009 | 1:30 pm

if you try to splice one element from an array, you still end up with an array (of one element) and not a number.
so the array (a) you created, is an array of one-element-arrays. max doesn’t unroll arrays of higher dimensions, so it outputs the elements of the first dimension, which are jsobjects (arrays in this case).

Quote:
Max should be smart enough to cast the array elements properly – it does it already via the post() function!

i’m not sure max does this cast when post() is called. what you see in the max window is a printout of one-element arrays.


April 28, 2009 | 6:47 pm

Uses comas to separate the js array and the string, otherwise the toString() method of the Array is used (if one of the operand of the + operator). post() and outlet() are clever enough to automatically unroll one level of an array.

post(theList, "n");

April 29, 2009 | 2:41 pm

No, EJ, I’m afraid they’re *not*. post() may be, but try the code I’ve posted and you’ll see that outlet() doesn’t unroll a list, it only converts it to a js object string. This is problematic, and not at all expected behavior. A list should be unrolled into a valus that Max can handle by default, not converted to a string representing the js object itself. For example, what good would it ever do to have a printout of the jsbojectXXXXX reference in Max? Output should be fully evaluated or else it is at best useless, at worst confusing and unexpected.

Cheers
Evan


April 29, 2009 | 11:56 pm

I can’t reproduce that using the latest version of Max with none of the above js. outlet() unroll the list as expected. Please attach an example and a patch just to make sure it’s not a character encoding issue of some sort. JS files are supposed to be UTF-8.


April 30, 2009 | 5:26 am

it a feature, not a bug Smile
as descriped before the splice function
does not return a number. its an array.
in your case with 1 element.

if you fill your array like this, everything works fine:

// this should add a number!
a.push(e[0]);

normaly max can not handle moredimensional arrays.
so its a feature, that the post method unrolls
the array Smile

in this case its a bit unclear, because of the different
handling between the post() and the outlet() function.


Viewing 9 posts - 1 through 9 (of 9 total)