HISE Logo Forum
    • Categories
    • Register
    • Login

    Array.find weird result

    Scheduled Pinned Locked Moved Scripting
    arrayfindarray.find
    4 Posts 2 Posters 272 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ustkU
      ustk
      last edited by ustk

      I'm trying to get my head around the find function but in this example I get a wrong result for sample == 0 (or negative values). But it works for other positive values...

      inline function searchInArray(sample)
      {
      	Console.print("> sample -> " + sample);
      	
      	local myArray = [0, 5, 10, 15, 20];
      	
      	local element = myArray.find(function[sample](currentValue, index, arr)
      	{
      		 Console.print("Value tested: " + currentValue + " | Is sample lower or equal? " + ((sample <= currentValue) ? ">TRUE" : "!FALSE"));
      		 
      		 if (sample <= currentValue)
      		 	return currentValue;
      		 
      	}, myArray);
      	
      	Console.print("> Element returned -> " + element); // spits the second element instead of first
      }
      
      searchInArray(0);
      

      Can't help pressing F5 in the forum...

      d.healeyD 1 Reply Last reply Reply Quote 0
      • d.healeyD
        d.healey @ustk
        last edited by

        @ustk Why not just use indexOf in this case? find is more useful when the array contains objects.

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        ustkU 1 Reply Last reply Reply Quote 0
        • ustkU
          ustk @d.healey
          last edited by

          @d-healey I'm looking for the case where sample is between two values in the array and report the one just below.
          Of course, I could just use a loop iterator in this example, but I'd like to master those find/map/some functions...

          Can't help pressing F5 in the forum...

          d.healeyD 1 Reply Last reply Reply Quote 0
          • d.healeyD
            d.healey @ustk
            last edited by

            @ustk

            Yeah I'd just use a loop. I'm not sure why find is not doing what's expected though... Also you don't need to set the second parameter as myArray because that's already there in the third parameter of the test function.

            This is what I would do

                local myArray = [0, 5, 10, 15, 20];
                local element; 
            	
                for (x in myArray)
                {
            	if (sample <= x)
            	{
            	    element = x;
            	    break;
            	}			
                }
            

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            1 Reply Last reply Reply Quote 0
            • First post
              Last post

            54

            Online

            1.7k

            Users

            11.7k

            Topics

            101.8k

            Posts