Javascript string comparison

Charles Vaughan's icon

Hi,

I basically need to do a simple string comparison, but it's taking me days and I don't know what I'm missing. Here's what I need:

array_1 [] - string inputs
array_2[] - string inputs

if one element (i) of array_1 == array_2
then
outlet(i, 1)
else
outlet(i,0)

I came up with this bit of javascript and for some reason every time I hit the comparison function it crashes Max completely. Feel like I'm missing something totally obvious here:

inlets = 2;
outlets = 6;

var userID = [] ;
var liveID = [] ;

function list() {
    if(inlet == 0) {
        userID = arrayfromargs(arguments);
    }

    if(inlet == 1) {
        liveID = arrayfromargs(arguments);
    }
}

function compare() {
    for(i = 0; i < liveID.length; i++) {
        for(j = 0; j < userID.length; i++) {

            if(liveID[i] == userID[j]) {
                outlet(i,1)
            } else {
                outlet(i,0)
            }
        }
    }
}

Ben Bracken's icon

I'm not totally sure what you want to do, but you weren't incrementing your var j. This iterates over the list coming in the right inlet, and compares each value with the list from the left inlet, and outputs a result of 0/1 for each test:

inlets = 2;
outlets = 6;
var userID = [];
var liveID = [];
function list() {    
    if (inlet == 0) userID = arrayfromargs(arguments);
    else liveID = arrayfromargs(arguments);
}
function compare() {
    for (var i = 0; i < liveID.length; i++) {
        for (var j = 0; j < userID.length; j++) {
            if (liveID[i] == userID[j]) outlet(i,1);
            else outlet(i,0);
        }
    }
}
Charles Vaughan's icon

Hey,

Apologies, I did a typo, there is a j in my code! Apologies.

I basically need the system to compare the list of thing in a with everything in b and if one of a is not in b, to switch off the associated toggle output.

florian1947's icon

try function anything() instead of list()

Charles Vaughan's icon

Nope changed it and it still crashes all of Max the second I hit the compare button, like I click it and then everything goes unresponsive immediatley.

hollyhook's icon

maybe you need to check the length of the lists? if it is too long, it will write on an invalid outlet...

florian1947's icon

If the lists are too long, Max will throw an error, but not crash.

I cant see what is wrong. Doesn't crash here. Testbed:

Max Patch
Copy patch and select New From Clipboard in Max.

Charles Vaughan's icon

Ok strange.

I checked the for loop length, changed it and still crashed my system. Got your code in and it didn't crash my system, but my javascript code doesn't appear to be doing what it should with that patch, it doesn't output a match even when I match them. Any thoughts?

florian1947's icon

I slightly modified your JS code, but didn't think it should make a difference:

compareStrings.js
application/x-javascript 0.66 KB

Perhaps you should show the relevant part of your max patch around the JS object; could be that the fault is there.

Jdudeo's icon

Ben how do you have semicolons in the middle of your if else statements without using brackets? some shorthand I've never heard of?

Ben Bracken's icon

@J Garcia You can leave out curly braces when there is only one statement. I use a general rule of only using curly braces for code blocks, and if there is just one statement, keeping it on one line. Some people probably think this is sacrilege, but I personally prefer it legibility purposes.

Jdudeo's icon

Thanks for the info, whatever works for you I guess

Charles Vaughan's icon

Hey guys,

Thanks for all the replies. So I scrapped all of my stuff and restarted copy pasting the above code and it work, no idea why it was crashing but hey ho.

Next problem.

So currently it works fine, my system is send a list to the left input, then to the right and then if I hit compare, if they match I get a nice toggle on. However, I need the javascript to detect when something in the left inlet doesn't match with the anything in the right inlet, and to switch the appropriate toggle.

For context, I'm using this to detect when users on the Mirar app disconnect. Everything on the left inlet is everyone's user IDs when they join, and on the right inlet it's the ongoing program. If someone disconnects, I need to know which user has disconnected. Now annoyingly, due to the for loop, when I add a '!=' condition or secondary if statement with this, if one thing in the second list doesnt matter with the item on the first list it turns off the toggle, even if there is another thing on the list before it that it does match with. It overrides the toggle information send.

How can I get past this? How can I tell it, only switch it off if the item from the first list does not match with ANYTHING in the second list, and only that?

Jdudeo's icon

should be pretty simple, in the inner loop create a variable as 0 and increment it when a match is found, if it's still 0 at the end of the loop then you had no matches

Charles Vaughan's icon

Thanks all working now! For reference here's the final code

inlets = 2;
outlets = 6;

var leftArray = [] ;
var rightArray = [] ;
var amnt = 0;

function list() {
    post();
    if(inlet == 0) {
        leftArray = arrayfromargs(arguments);
    }

    if(inlet == 1) {
        rightArray = arrayfromargs(arguments);
    }
}

function RESET() {
    for(i = 0; i < 6; i++) {
        outlet(i,0)
        }
        amnt = 0;
    }


function compare() {
    for(i = 0; i < leftArray.length; i++) {
        var c = 0;
        for(j = 0; j < rightArray.length; j++) {
            
            if(leftArray[i] == rightArray[j]) {
                outlet(i,1)
                c += 1;
            }
            
            if(leftArray[i] != rightArray[j] && c == 0) {
                outlet(i, 0)
            }
        }
    }
}
Jdudeo's icon

glad it works, I was thinking that the outlet calls would be outside the j for loop though, looks like you'll get unnecessary duplicate messages coming out of your js object. Try this:

function compare() {
    for(i = 0; i < leftArray.length; i++) {
        var c = 0;
        for(j = 0; j < rightArray.length; j++) {
            
            if (leftArray[i] == rightArray[j]) c++;    
        }
        c > 0 ? outlet(i,1) : outlet(i,0);
    }
}