compare a number with a structure

xniala's picture

Hello,

A Queue stores values (several numbers between 0 et 99 entering one by one) I want to know if and when this value is repeating (same number comes again)

in other words, can I easily compare the last value with each older in the structure generated by the Queue ?

I made a macro patch with 30 times : structure index member > conditional set with each index... It's so big...

The solution is certainly simpler...

Thanks, Alain

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

xniala's picture
Re: compare a number with a structure

Hello, I'm working again on this project these days and I'm still wondering how to compare values with structures.

To be more specific : how to compare values with other values within a structure.

For example I want to know :

  • if a value is repeated
  • if yes, when ? Which rank (first, second... value entered)
  • if a new value is the lowest, of these already contained in the structure
  • ...

I'm really stuck and I'm surprised not to find a "search in Structure" patch in QC...

Thanks for any help ! Alain

Achim Breidenbach's picture
Re: compare a number with a structure

My suggestion is to use JavaScript (or LUA script with 3rd party plugin) to compute this kind of logic. QC isn't made for this complex logic and so its necessary to use a high level programming language.

xniala's picture
Re: compare a number with a structure

Thanks Achim for your answer. I've no experience with Javascript, so have you any advices regarding my needs before I try something ? Links, reading...

Thanks again Alain

Achim Breidenbach's picture
Re: compare a number with a structure

Alain,

sorry, I don't have any special links. However this may give you some startingpoint:

How to get the max-value of an structure: (DISCLAIMER: Untested code!)

function (__number max) main (__structure array)
{
   arrayMax = 0;
   for (i = 0; i < array.length(); ++i)
   {
      if (array[i] > arrayMax)
      {
         arrayMax = array[i];
      }
   }
   var result = new Object();
   result.max = arrayMax;
   return result;
}

Here is some more about JavaScript in QC: http://kineme.net/wiki/JavaScriptQuartzComposer

xniala's picture
Re: compare a number with a structure

Waou ! Great !

I'll try this today and also try to adapt your code for : - duplicate entries - rank of entries ...

Thanks, Alain

xniala's picture
Re: compare a number with a structure

Maybe I was too hopeful, I don't have enough programming skills to succeed in my goals...

I try to combine your code and something I found here : http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-...

Without any result. I must stop for today, but will try again tomorrow.

Thanks again, Alain

by the way, QC shows a TypeError line 4 : "nul is not an object (evaluating array.length) But I'm not sure to understand what it means...

Achim Breidenbach's picture
Re: compare a number with a structure

My JavaScript is a bit rosty. ;-)

You have to wrap everything with "if (!_testMode)" because the JavaScript is run in a preflight testmode with no input. So there is no array coming in and therefor the .length() fails.

function (__number max) main (__structure array)
{
   arrayMax = 0;
   if (!_testMode)
   {
      for (i = 0; i < array.length(); ++i)
      {
         if (array[i] > arrayMax)
         {
            arrayMax = array[i];
         }
      }
  }
   var result = new Object();
   result.max = arrayMax;
   return result;
}

xniala's picture
Re: compare a number with a structure

I'll test this as soon as possible.

Thanks again, Alain

xniala's picture
Re: compare a number with a structure

Unfortunately I've not enough time to dig in and start learning JS these days. But it's definitively something I'll have to do one day or the other...

For now I found a way to solve my problem with "Mathematical Expressions". It's not really sophisticated, but it works.

Thanks again Achim for your time !

usefuldesign.au's picture
Re: compare a number with a structure

Math Expression Patch is the best. Kineme have a turbo charged version that allows for multiple expressions and results with one set of inputs. No sure which plugin it is, maybe Data Tools?

There are stock sort structure and structure range patches which allow for reasonably good handling of structures too. Nothing like you'll get with a scripting language.

I've added a composition to get you started. It generates a structure of random numbers, sorts them then finds the matches and highlights them in the window. All without Javascript or Lua :-)

There are two user inputs at the top level for flushing the random structure and number of array elements.

PreviewAttachmentSize
Sort Structure patch demo.qtz78.17 KB

xniala's picture
Re: compare a number with a structure

Thanks, I'll look at this composition very soon.