comparing lists from two inlets in JavaScript


    Dec 28 2013 | 3:41 pm
    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)

    • Dec 28 2013 | 4:52 pm
      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 ; 
      	}
      }
      
      
      
    • Dec 28 2013 | 5:06 pm
      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.
    • Dec 28 2013 | 5:36 pm
      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 ;
      }
      
      
    • Dec 28 2013 | 6:11 pm
      ... 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 ?
    • Dec 28 2013 | 6:23 pm
      i believe that you can do similar things with "zl" objects in max directly . check zl.compare ,seems pretty useful !
    • Dec 28 2013 | 7:17 pm
      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 :)
    • Dec 28 2013 | 8: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.
    • Dec 28 2013 | 9: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
    • Dec 28 2013 | 9:30 pm
      Thanks :)
    • Dec 28 2013 | 9:42 pm
      :)))))))))
    • Jan 08 2014 | 8: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?
    • Jan 08 2014 | 8: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