Count appearances of a value in a structure

zuvala's picture

hi there

Is it possible to detect how many times a value is repeated within a structure? ie. i've got a xml-file with a "date" key and want to create several individual representations grouped for each day.

Or divide a structure in several substructures, when a key value changes?

thanks for any advice

Comment viewing options

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

zuvala's picture
Re: Count appearances of a value in a structure

I've got a solution with javascript, but i bet you guys know better.

Some information about the project: I try to load a xml-file with all my sms-messages and arrange them as an interactive infographic. Now my composition sorts the structure by the phone number and then the following javascript regroups the messages with an additional node called "chat":

/*
A simple script that takes two input values, sums them and returns the result.
 
Special keywords for defining input and output key types:
__boolean, __index, __number, __string, __image, __structure, __virtual
 
Note that the function input arguments are read-only.
*/
 
function (__structure OutputStructure, __string StructureLabel) main (__structure InputStructure, __string SplitAttribute)
{
   result = new Object();
 
   if (!_testMode) {   
 
      // select the correct attribute
      var splitter = 0;         
      switch(SplitAttribute) {
         case "address": splitter = 1; break;
         default: splitter = 0;
      }      
 
      var build = new Object();
         var c1 = 0;
         var c2 = 0;
      var count1 = InputStructure.length;   
      var label = "nothing";
 
      // reorder associative array
      for (i=0;i<count1;i++) {
         if(i == 0) {
            build["chat"+c1] = new Object();
            build["chat"+c1].sms = InputStructure[i];
         }
 
         if(i > 0) {
            if(InputStructure[i][1] != InputStructure[i-1][1]) {
               c1++;
               c2 = 0;
               build["chat"+c1] = new Object();
               build["chat"+c1].sms = InputStructure[i];
            } else {
               c2++;
               build["chat"+c1]["sms"+c2] = InputStructure[i];
            }
         } 
      }
 
      // number of total chats
      var size = 0, key;
          for (key in build) {
              if (build.hasOwnProperty(key)) size++;
          }
      var count2 = size;
   }
       result.OutputStructure = build;
   result.StructureLabel =  "sms: " + count1 + " in chats " + count2;
     return result;   
}