JSUI : question about Array index/value

Oct 3, 2007 at 2:09pm

JSUI : question about Array index/value

Hi,

I’m working on a JSUI knob meant for a compressor knee.

SO I’ve setted an Array with my knee settings :

var Ratio = ["1:1", "2:1", "3:1","4:1", "6:1", "10:1", "];

My goal is to be able to control this jsui knob by sending message of the knee settings (ex:10:1), rather than float or int (i.e. NOT by sending the array index).

Thus my question :

Is it possible to send a “value” to an Array, so it check if it match the content, and if positive retrieve the array index for that “value” ?

ie. for

var Ratio = ["1:1", "2:1", "3:1","4:1", "6:1", "10:1"];
// index’s are 0 1 2 3 4 5

I send the message “3:1″ and I get back the index of this value wich is “2″.

I’ve tried with vregexp.exec(Ratio) etc… but I lack skill in java and I have no good results.

So far, I’m doing like this :

function ratio(input)

{
switch (input)
{ case “1:1″: msg_float(0) ;break;
case “2:1″: msg_float(1) ;break;
case “3:1″: msg_float(2) ;break;
case “4:1″: msg_float(3) ;break;
case “6:1″: msg_float(4) ;break;
case “10:1″: msg_float(5) ;break;
case “20:1″: msg_float(6) ;break;
case “NUKE”: msg_float(7) ;break;
}
}

This works, but I want to use this code on much bigger arrays(like for a fixed frequency knob with dozen of frequency), and it’ll be hard to manage all those cases lines. Especially if I make change in the array order and number etc…

Second question :
Actually I have to pass a message with a prefix “ratio 3:1″ , to enter the function ratio(input).

Is there anyway to make it work just by sending a single symbol “3:1″ ? Is is possible at all to send single symbol message to jsui ?

Thanks for any help or advice !

Salvator

#33943
Oct 3, 2007 at 2:38pm

I’m no expert either, but I’d have thought something along the lines of this
would be work:

var Ratio = ["1:1", "2:1", "3:1","4:1", "6:1", "10:1"];

function ratio(a)
{
for(i=0; i
{
if(Ratio[i] == a)
{
outlet(0, i);
}
}
}

If you send “ratio 3:1″ you’ll get back “2″ out the outlet.

max v2;
#N vpatcher 15 55 615 455;
#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P message 214 132 21 9109513 4:1;
#P message 190 132 21 9109513 3:1;
#P newex 190 157 62 9109513 prepend ratio; #P number 190 203 35 9 0 0 0 139
0 0 0 221 221 221 222 222 222 0 0 0; #P newex 190 180 41 9109513 js ratios;
#P connect 3 0 2 0; #P connect 4 0 2 0; #P connect 2 0 0 0; #P connect 0 0 1
0; #P pop;

Hope that’s of use,
Simon

—–Original Message—–
From: javascript-dev-bounces@cycling74.com
[mailto:javascript-dev-bounces@cycling74.com] On Behalf Of Salvator
Sent: 03 October 2007 15:10
Subject: [javascript-dev] JSUI : question about Array index/value

Hi,

I’m working on a JSUI knob meant for a compressor knee.

SO I’ve setted an Array with my knee settings :

var Ratio = ["1:1", "2:1", "3:1","4:1", "6:1", "10:1", "];

My goal is to be able to control this jsui knob by sending message of the
knee settings (ex:10:1), rather than float or int (i.e. NOT by sending the
array index).

Thus my question :

Is it possible to send a “value” to an Array, so it check if it match the
content, and if positive retrieve the array index for that “value” ?

ie. for

var Ratio = ["1:1", "2:1", "3:1","4:1", "6:1", "10:1"];
// index’s are 0 1 2 3 4 5

I send the message “3:1″ and I get back the index of this value wich is
“2″.

I’ve tried with vregexp.exec(Ratio) etc… but I lack skill in java and I
have no good results.

So far, I’m doing like this :

function ratio(input)

{
switch (input)
{ case “1:1″: msg_float(0) ;break;
case “2:1″: msg_float(1) ;break;
case “3:1″: msg_float(2) ;break;
case “4:1″: msg_float(3) ;break;
case “6:1″: msg_float(4) ;break;
case “10:1″: msg_float(5) ;break;
case “20:1″: msg_float(6) ;break;
case “NUKE”: msg_float(7) ;break;
}
}

This works, but I want to use this code on much bigger arrays(like for a
fixed frequency knob with dozen of frequency), and it’ll be hard to manage
all those cases lines. Especially if I make change in the array order and
number etc…

Second question :
Actually I have to pass a message with a prefix “ratio 3:1″ , to enter the
function ratio(input).

Is there anyway to make it work just by sending a single symbol “3:1″ ? Is
is possible at all to send single symbol message to jsui ?

Thanks for any help or advice !

Salvator

#113729
Oct 3, 2007 at 2:46pm

That’s odd. Just incase you’re looking at the forum view not the email list here’s the js code again with out the bits missing… ?!

var Ratio = ["1:1", "2:1", "3:1","4:1", "6:1", "10:1"];

function ratio(a)
{
for(i=0; i
{
if(Ratio[i] == a)
{
outlet(0, i);
}
}
}

Simon

#113730
Oct 3, 2007 at 2:59pm

On 3 oct. 07, at 16:46, simon adcock wrote:

> That’s odd. Just incase you’re looking at the forum view not the
> email list here’s the js code again with out the bits missing… ?!
>
> var Ratio = ["1:1", "2:1", "3:1","4:1", "6:1", "10:1"];
>
> function ratio(a)
> {
> for(i=0; i
> {
> if(Ratio[i] == a)
> {
> outlet(0, i);
> }
> }
> }

One small thing, don’t forget to declare the “i” variable with the
var keyword, otherwise it becomes global.

ej

#113731
Oct 4, 2007 at 7:18am

Thanks, It works !!

Emmanuel,
I’m nt sure to understand … You mean just adding “var i=0″ inside the function ?

Any idea about my question #2 :
Actually I have to pass a message with a prefix “ratio 3:1″ , to enter the function ratio(input).

Is there anyway to make it work just by sending a single symbol “3:1″ ? Is it possible at all to send single symbol message to jsui ?

Salvator

#113732
Oct 4, 2007 at 8:08am

On 4 oct. 07, at 09:18, Salvator wrote:

> Thanks, It works !!
>
> Emmanuel,
> I’m nt sure to understand … You mean just adding “var i=0″ inside
> the function ?

if you don’t use the var keyword, the variable is automatically
declared as global (in the JavaScript meaning, not the Global
object). And it’s usually very hard to figure out the problem (for
example, if you have 2 jsui running at the same time, they will share
the i variable, which is really not what we want in such situation).

> Any idea about my question #2 :
> Actually I have to pass a message with a prefix “ratio 3:1″ , to
> enter the function ratio(input).
>
> Is there anyway to make it work just by sending a single symbol
> “3:1″ ? Is it possible at all to send single symbol message to jsui ?

Have a look to the anythinng() method. The message name property
should have the symbol “3:1″. Although I didn’t test, the following
should work:

var Ratio = ["1:1", "2:1", "3:1","4:1", "6:1", "10:1"];

function anything()
{
var i; // this is really important ;-)

for(i = 0; i < Ratio.length; i++) {
if(Ratio[i] == messagename)
outlet(0, i);
}
}

ej

#113733
Oct 4, 2007 at 9:40am

It Works !

Your help is much much apprectiated : Thanks again !

Salvator

#113734

You must be logged in to reply to this topic.