1

Numeric array variable initialization

Hello there.

I'm stuck with a somehow simple problem. I can't create a variable type array and initialize it unless I enter a number. Take a look at this code:

let myArray := [];
for x from 1 to 10 do
    let newNumber := [number(x)];
    myArray := array(myArray, newNumber)
end;
concat(myArray)

It returns nothing. But add a 0 to the array definition, like this:

let myArray := [0];
for x from 1 to 10 do
    let newNumber := [number(x)];
    myArray := array(myArray, newNumber)
end;
concat(myArray)

And it returns a list of numbers:

0,1,2,3,4,5,6,7,8,9

So, how can I initialize a numeric array WITHOUT inserting any number in the array definition?

NOTE: For initializing an string array, there is already an easy method; you just type an empty string:

let myTextArray := [""];

9 replies

null
    • Ninox developper
    • Jacques_TUR
    • 1 yr ago
    • Reported - view

    Ninox needs to know the exact type of variable to set it. You cannot create a variable with an unknown type as JavaScript.
    About your code, you can simplify it, then you don't have to initialize with [0] : 

    let myArray := for x from 0 to 10 do
        x;
    end;
    concat(myArray);
    
      • Consultant and developer
      • Javier
      • 1 yr ago
      • Reported - view

      Jacques TUR Thanks for your quick reply. The above code was a simple example. The real code is more or less something like this:

      let xItinerario := Itinerario;
      let xCircuitos := ((select Itinerario_Circuitos where Itinerario = xItinerario) order by Orden);
      let xListaMonedas := [0];
      for x from 0 to count(xCircuitos) do
          let xCircuito := item(xCircuitos, x);
          let xNumProveedor := number(xCircuito.Proveedor);
          let xProveedor := (select Proveedores where 'Nr.' = xNumProveedor);
          let xMoneda := [number(xProveedor.Moneda)];
          xListaMonedas := array(xListaMonedas, xMoneda)
      end;
      xListaMonedas := slice(xListaMonedas, 1, count(xListaMonedas));
      

      As you see, xListaMonedas is initialized with a 0 and  then I need to slice the first value (the 0) in order to get the final "clean" array. Is there a "proper" solution?

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Javier Try this code : 

      let xItinerario := Itinerario;
      let xCircuitos := ((select Itinerario_Circuitos where Itinerario = xItinerario) order by Orden);
      
      "// create empty array of record";
      let xListaMonedas := select Moneda where id = -1;
      
      "// Through all the elements of XCircuitos"
      for xCircuito in xCircuitos do
          let xNumProveedor := number(xCircuito.Proveedor);
          let xProveedor := (select Proveedores where 'Nr.' = xNumProveedor);
          let xMoneda := [number(xProveedor.Moneda)];
          xListaMonedas := array(xListaMonedas, xMoneda)
      end;
      
      • Consultant and developer
      • Javier
      • 1 yr ago
      • Reported - view

      Jacques TUR Great and clean! Thank you very much! I like very much this special way of iterate an array:

      for xCircuito in xCircuitos do [...]

      The documentation is not very extensive teaching how to solve these kind of problems, or the many ways Ninox can iterate an array, the type of data a Select returns, how to work with the received data of a query, etc...

      Thanks again for your help.

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Javier Another solution is to initialize array of number filtered with no result (an impossible result) :

      let xListaMonedas := let myArray := [0][ = 1];
      

      like this exemple :

      let myArray := [0][ = 1];
      for i from 0 to 10 do
          myArray := array(myArray, [i]);
      end;
      debugValueInfo(myArray);

      return : number([0,1,2,3,4,5,6,7,8,9])

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Javier See also this post about filtering arrays : https://forum.ninox.com/t/y4hbx90/acces-to-array-items-like-with-where-fonction

      And this about kind of array types possible with record id : https://forum.ninox.com/t/m1hwj3v?r=m1hwjc1

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Javier The exact syntax to create one typed empty array is :

      let numberArray := [0][false];
      let textArray := [""][false];
      let JSONArray := [{}][false];
      let recordArray := select myTable[false];
      
      let unknownArray := [][false];
      
    • Sean
    • 1 yr ago
    • Reported - view

    After Günther posted this solution I made a test database with some additional array manipulations. I can't test it against your code right now, but it might help.

      • Consultant and developer
      • Javier
      • 1 yr ago
      • Reported - view

      Sean Thanks Sean! I'll give a try to your code!

Content aside

  • 1 Likes
  • 1 yr agoLast active
  • 9Replies
  • 328Views
  • 3 Following