Parse arguments and convert int to string problems

Jul 23, 2010 at 5:21am

Parse arguments and convert int to string problems

Hi,

I have previously worked with JavaScript in web applications

MaX I encountered some problems when trying to convert an integer variable to a string and get the values of an array in a loop.

My function takes as input an array generated by the function (pack 0,0,0,0,0), trying to get the values of each array elements value assigned to the temporary variable “a” function stops working.

first attempt

if (arguments.length)
(

nargs = arguments.length;
post (“nargs” nargs, ” n”);

for (i = 0; i
(
a = arguments [i];
post (“a”, a =, ” n”);

)

)

given the problem change the following code, the solution is inelegant and trying to convert the temporary variable “a” to chain the function stops working

if (arguments.length)
(

var a = 0:
var z = “”;
nargs = arguments.length;
post (“nargs” nargs, ” n”);

for (i = 0; i
(
if (i == 0) (
a = arguments [0];
)
if (i == 1) (
a = arguments [1];
)
if (i == 2) (
a = arguments [2];
)
if (i == 3) (
a = arguments [3];
)
if (i == 4) (
a = arguments [4];
)

/ / Concatenate the values
z = z + a.toString ();

)

post (“z =” z, ” n”);

)

if anyone can help me with these questions or pass the Max JS reference that uses the very grateful.

Cheers

Paultool

#51481
Jul 23, 2010 at 11:22am

What exactly are you trying to do? The first example set the variable “a” to each value in the input list but doesn’t do anything with it other than print it to the max window. The second example does the same but the printing is done outside of the loop so only the last argument is shown.

A quick tip: post the exact javascript code so we can check it easily, in this case use {curly braces} to denote blocks not (parentheses) so we don’t have to change it to run a test.

Explain what you’re putting in and what you want out the other end a bit more clearly and I’ll try to help.

lh

#184532
Jul 23, 2010 at 10:40pm

Thanks for the reply Luke

I need is to get the values of each of the incoming arguments under, taking these values generated a string with zeros and ones.

The problem I have in the first instance is when I try to get the values of the input.

1 Error

for (i = 0; i
{
a = arguments [i]; // this dont work
post (“a”, a =, ” n”);

}

The second problem is when I try to convert an integer variable to string

2. Error

z = a.toString (); // this dont work

none of these forms work, then show the complete code in this Encounter me problems when I try to concatenate

z = a1 + a2 + a3 + a4 + a5 +a6 +a7 +a8; // dont work

My code:

// inlets and outlets
inlets = 1;
outlets = 1;

function list(val)
{

//setup initial vars
var a =0;
var z = “”;
var va1=”0″;
var a2=”0″;
var a3=”0″;
var a4=”0″;
var a5=”0″;
var a6=”0″;
var a7=”0″;
var a8=”0″;
var a9=”0″;

if(arguments.length) // bail if no arguments
{

niputs=arguments.length;
post (“niputs”, niputs, “n”);

for(i=0;i
{

if(i==0){
a = arguments[0];
}
if(i==1){
a = arguments[1];
}
if(i==2){
a = arguments[2];
}
if(i==3){
a = arguments[3];
}
if(i==4){
a = arguments[4];
}

// bin args
if(a==1){
a1=’1′;
}
if(a==2){
a2=’1′;
}
if(a==3){
a3=’1′;
}
if(a==4){
a4=’1′;
}
if(a==5){
a5=’1′;
}
if(a==6){
a6=”1″;
}
if(a==7){
a7=’1′;
}
if(a==8){
a8=’1′;
}
if(a==9){
a9=’1′;
}

}

//concatenate bin args
z = a1 + a2 + a3 + a4 + a5 +a6 +a7 +a8; // dont work
post (“i”, z, “n”);

outlet(0,z);
}

}

Thank for all

Paultool

#184533
Jul 23, 2010 at 10:51pm

I use MAX 5.0.7 for windows

Correction

My code:

// inlets and outlets
inlets = 1;
outlets = 1;

function list(val)
{

//setup initial vars
var a =0;
var z = “”;
var a1=”0″;
var a2=”0″;
var a3=”0″;
var a4=”0″;
var a5=”0″;
var a6=”0″;
var a7=”0″;
var a8=”0″;
var a9=”0″;

if(arguments.length) // bail if no arguments
{

niputs=arguments.length;
post (“niputs”, niputs, “n”);

for(i=0;i
{

if(i==0){
a = arguments[0];
}
if(i==1){
a = arguments[1];
}
if(i==2){
a = arguments[2];
}
if(i==3){
a = arguments[3];
}
if(i==4){
a = arguments[4];
}

// bin args
if(a==1){
a1=’1′;
}
if(a==2){
a2=’1′;
}
if(a==3){
a3=’1′;
}
if(a==4){
a4=’1′;
}
if(a==5){
a5=’1′;
}
if(a==6){
a6=”1″;
}
if(a==7){
a7=’1′;
}
if(a==8){
a8=’1′;
}
if(a==9){
a9=’1′;
}

}

//concatenate bin args
z = a1 + a2 + a3 + a4 + a5 +a6 +a7 +a8; // dont work
post (“i”, z, “n”);

outlet(0,z);
}

}

Thank for all

Paultool

#184534
Jul 24, 2010 at 3:13pm

I’m still not quite sure what you are trying to achieve. The only reason you’d have a problem with your first piece of code is there is an error in the post() function. The code below will work if you send it a list:

function list() {
    if (arguments.length) {
        nargs = arguments.length;
        post ("nargs =",nargs,"n");
        for (i = 0; i 

The second part where you are doing concatenation of strings should work too, here's some more example code:

var a1 = "1";
var a2 = "2";
var a3 = "3";

function bang() {
    answer = a1+a2+a3;
    post(typeof(answer),"n");
    post(answer,"n")
}

What lists are you sending to your javascript object and how are you expecting them to be formatted when they are sent from the outlet? Reply with a quick example ie:

In: 1 2 3 4 5
Out: "01011"

This will hopefully give me a better understanding of your problem.

#184535

You must be logged in to reply to this topic.