Forums > Javascript

comparing lists from two inlets in JavaScript

December 28, 2013 | 7:41 am

Hi

I want to build a JS object that has two inlets
- each inlet gets a list of varying length’s (numbers)
- I then want to compare list a to list b, – so I guess I need to sort the list (making the smallest number come in front, and etc.) but that could also be done before the js object (since I read somewhere that JS in Max doesn’t have a sort method ?)
- I want to get TRUE if the shorter list matches the longer list’s first numbers (so if list a is (1,2,3) and list b is (1,2,3,4,5) then that should return True (or bang)

I understand how to make a list() function – something like this :

function list()
{
for (i=0; i < arguments.length; i++)
{
post("item ", i+1, "is", arguments[i]+",");
}
}

But I don't know how to go about making a function that takes two lists from two inlets – how do I discern which list is being refered to when I use "arguments"

I've gone through the javascript helpfiles Max offers (which in all honesty are confusing and over-written in my opinion (Max usually having great help files in my opinion)) – but there is very little to be learned there about comparing input data , and even less about how to use more than one inlets (other than, apparently, you can)


December 28, 2013 | 8:52 am

im not at home ,so i cant check it , but it could work for you

function list(){
	if(inlet === 0){
		arr_a = arrayfromargs(arguments) ;
		arr_a.sort(function(a,b){return a-b;})
	}

	if (inlet === 1){
		arr_b = arrayfromargs(arguments) ;
		arr_b.sort(function(a,b){return a-b;})
	}
		if( doCompare() ) outlet(0,"bang");
}

function doCompare(){
	var i , count = 0 ;
	var len_a = arr_a.length ;
	var len_b = arr_b.length ;

	if(len_a >= len_b){
		for(i = 0 , i < len_b ; i++){
			if(arr_b[i] == arr_a[i]){
				count++ ;
			}
		}
		return count == len_b ; 
	}else{
		for(i = 0 , i < len_a ; i++){
			if(arr_a[i] == arr_b[i]){
				count++ ;
			}
		}
		return count == len_a ; 
	}
}

December 28, 2013 | 9:06 am

Hi, Thanks a lot for the reply

I get a: Javascript TypeError. ArgumentsInlet is undefined, line 9
and : error calling function list

… line nine is an empty line btw.

Judging from a pure JavaScript perspective your code should work, it seems to me.


December 28, 2013 | 9:36 am

yes , im stupid

autowatch = 1;
inlets = 2 ;
outlets = 1 ;
var arr_a = [] ;
var arr_b = [] ;

function list(){

	if(inlet === 0){
		if(arguments.length){
			arr_a = arrayfromargs(arguments) ;
			arr_a.sort(function(a,b){return a-b;});
		}

	}

	if (inlet === 1){
		if(arguments.length){
			arr_b = arrayfromargs(arguments) ;
			arr_b.sort(function(a,b){return a-b;});
		}

	}
		if( doCompare(arr_a,arr_b) ) outlet(0,"bang") ;

}

function doCompare(a,b){
	var i , count = 0 ;
	var len_a = a.length ;
	var len_b = b.length ;
	if(len_b && len_a){
		if(len_a >= len_b){
			for(i = 0 ; i < len_b ; i++){
				if(b[i] == a[i]){
					count++ ;
				}
			}
			return count == len_b ; 
		}else{
			for(i = 0 ; i < len_a ; i++){
				if(a[i] == b[i]){
					count++ ;
				}
			}
			return count == len_a ; 
		}		
	}
	return false ;
}

December 28, 2013 | 10:11 am

… You seem pretty smart to me

Thanks a lot! :)

I am wondering… since I am going to have a long list of arrays that I want to compare, and that long list is constantly updating – would it be best to just use coll and have a counter run it – or is there some better compact way, using JS ?


December 28, 2013 | 10:23 am

i believe that you can do similar things with "zl" objects in max directly . check zl.compare ,seems pretty useful !


December 28, 2013 | 11:17 am

Yeah, I tried using that, but it doesn’t compare lists of differing lengths

I think the js version will be more apt.

Thanks again :)


December 28, 2013 | 12:46 pm

Sorry to be a pest … but I am wondering if you can explain a bit what your are doing with this line => arr_b.sort(function(a,b){return a-b;});

The a-b part particularly.


December 28, 2013 | 1:06 pm

Hi . dont worry
yes this looks crazy . its a common comparison method for sort() function . if you really want to dig into it then look here ..

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort


December 28, 2013 | 1:30 pm

Thanks :)


December 28, 2013 | 1:42 pm

:)))))))))


January 8, 2014 | 12:15 pm

@edsonedge what do you mean by zl.compare doesn’t work when the list have different size? Since they have different size, they are not equal so zl.compare outputs 0. Am I missing something?


January 8, 2014 | 12:24 pm

I needed to get a comparison function that gets a bang(True) – as long as the longer list contains all the index numbers of the shorter list

But the longer list can then have extra numbers


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