Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Comparing 4 columns against 4 other columns values. Efficient way to search for true?

    First off, I want to thank everyone who has replied to my previous posts. I truly appreciate your time and effort.

    I have a test that I am doing on some bitwise values...okay (have this now because someone posted a great solution)...but I am wondering if there is an effective way to compare 4 column values to 4 other column values in an array in an effective manner instead of doing a for loop...

    for example..

    var red = new Array[red car, [0,1,4, 2]]

    var blue = new Array[blue car, [1,1,0,7]]
    var green = new Array[green car, [2,1,4,16]]

    The idea here is to test, lets say, the 'red' array red[1], its four values against the other arrays (blue, green) 4 columns....but instead of looping thru the arrays and then comparing - again, lets say - red[1][0] etc..to blue[1][0] --> if they are equal, move to the next element in the array for comparision..

    is there an effective way to almost use a "layer" and compare all 4 elements at once? And if all match, it returns a true, else a false....perhaps a math oprerator or some sort of custom object

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Try:
    Code:
    if (red[1].toString() == blue[1].toString()){
        //elements in red and blue arrays are equal
    }

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I understand your answer...but i am comparing for bitwise, else I could just concatenate the array and do a test for the string.......

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values
    I understand your answer...but i am comparing for bitwise, else I could just concatenate the array and do a test for the string.......
    What string?

    Is this such super secret code that you can not provide an example?
    For this is at least the second thread you started asking for specifics upon a general question without providing any code to work from.

    Code please......

    Edit: BTW: For anyone interested and wishing to be brought up to speed.
    Here is a link to at least one thread where this has been discussed previously.
    Last edited by Willy Duitt; 06-22-2004 at 02:26 AM.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    there is no super secret code...I am architecturing it now...

    but for me to create this "compare" function for the bitwise....I need the code ideas...

    I can just reiterate thru the array and compare with another array...but I was hoping there'd be some unique way of comparing 4 bitwise to another 4 bitwise and getting a true if all are equal, else returns a false...with going thru a FOR loop...perhaps there was something about bitwise I dno't know...like if you add them all up you can just then compare both sums...ala...

    bitwisearrayONE(16,1,0,0)

    compared to:

    bitwisearrayTWO(1,4,1,0)

    compare 16 to 1
    compare 1 to 4
    compare 0 to 1
    compare 0 to 0

    if any of that test returns a false, then break ---> return false
    else, if its all true - return true....

    for(i=0;i<bitwisearrayONE.length;i++){
    x=bitwisearrayONE[i];
    y=bitwisearrayTWO[i];
    b=new Boolean(x & y)
    if(b != true){
    return false;
    }



    I could do something like that...but I am looking for something "more" creative in which I can compare ALL 4 items at once....

    Also, the "STRING" I am referring to is the method "toString" that a previous poster coded.
    Also, I posted previously on a different solution for a similiar topic....comparing 2 bitwise. This is different. I CAN compare as many bitwise as needed, what I am looking for is a unique way of doing it without using FOR loops...that is all.
    Last edited by BrightNail; 06-22-2004 at 06:37 AM.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    in which I can compare ALL 4 items at once....
    I confees it looks confusing to me you demand...

    You have two arrays. You want to compare bitwise the arrays elements. It is obvious that you have to compare the elements one by one, as bitwise operator are biunivoque, that means the comparation is done first between 2 elements, than between the other 2 and so on

    So
    var arr1= 1
    var arr2= 1
    var arr3= 1
    alert(arr1==arr2==arr3)

    will return true, while

    var arr1= 1
    var arr2= 1
    var arr3= 2
    alert(arr1==arr2==arr3)

    will return false

    This is the reason why if extended this type of comparation to arrays
    var arr1= new Array(1,2,3,4)
    var arr2= new Array(1,2,3,4)
    var arr3= new Array(1,2,3,4)
    alert(arr1==arr2==arr3)

    will return, to your surprise, false.

    You see that the arrays are the same, from your point of view, but you forget that bitwise comparation works between all the elements af all the arrays, thus at a moment 1 from one array is compared with 2 from another array which, obviously will give a boolean false value. See also

    var arr1= new Array(1)
    var arr2= new Array(1)
    var arr3= new Array(1)
    alert(arr1==arr2==arr3)

    Now of course, the return will be true.

    In a word, to compare bitwise two arrays you must compare each correspondent (by order) element, you must have as many bitwise comparations as the number of arrays elements (array's length). Of course, you need also that the arrays length to be the same.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You must see also that an new array is an object not a number or a string thus a full direct bitwise comparasion between arrays is either impossible (as the comparation will be automaticaly be made between all the elements from all order levels) or nonsense, see below

    var arr1= new Array(1,2,3)
    var arr2= new Array(4,5)
    alert(toString(arr2)==toString(arr1))

    will return, to your surpise true even the arrays have different elements and different length. That is understandable if you:

    alert(toString(arr1))

    and notice that the result is [object]
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    In conclusion, as far as my knowledge is, you can compare 2 or more arrays using something like:

    var arr1= new Array(1,2,3)
    var arr2= new Array(1,2,3)
    var arr3= new Array(3,4,5)
    for(var i=0;i<arr1.length;i++){
    var bitwise12 = (arr1[i]==arr2[i])
    var bitwise13 = (arr1[i]==arr3[i])
    var bitwise23 = (arr2[i]==arr3[i])
    }
    alert(' Arrays 1 and 2 are: '+bitwise12+' Arrays 1 and 3 are: '+bitwise13+' Arrays 2 and 3 are: '+bitwise23)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Regular Coder
    Join Date
    Jun 2004
    Location
    underground
    Posts
    186
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    You must see also that an new array is an object not a number or a string thus a full direct bitwise comparasion between arrays is either impossible (as the comparation will be automaticaly be made between all the elements from all order levels) or nonsense, see below

    var arr1= new Array(1,2,3)
    var arr2= new Array(4,5)
    alert(toString(arr2)==toString(arr1))

    will return, to your surpise true even the arrays have different elements and different length. That is understandable if you:

    alert(toString(arr1))

    and notice that the result is [object]

    The correct syntax for that method is: objectname.toString([radix])

    alert(arr1.toString() == arr2.toString()) will return an appropriate result.

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yes, indeed, neofibril is right, thus using array.toString() you can compare bitwise two arrays without looping through the elements

    var arr1= new Array(1,2,3)
    var arr2= new Array(1,2,3)
    alert(arr1.toString()==arr2.toString())

    That means this could be the method... my mistake was to mixt the syntax, but as general view I think I am right, anyway, the bitwise comparation of arrays can be done only two by two in a biunivoque way

    var arr1= new Array(1,2,3)
    var arr2= new Array(1,2,3)
    var arr3= new Array(3,4,5)
    var bitwise12 = (arr1.toString()==arr2.toString())
    var bitwise13 = (arr1.toString()==arr3.toString())
    var bitwise23 = (arr2.toString()==arr3.toString())
    alert(' Arrays 1 and 2 are: '+bitwise12+' Arrays 1 and 3 are: '+bitwise13+' Arrays 2 and 3 are: '+bitwise23)

    That is not a shorter code, I reckon, as bitwise operator will loop by itself through the arrays elements.

    Is this the solution? What do you think?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •