0

Loops syntax

Hi, guys:

I'm running into something that I just can't get past.  I have a loop structure that I can get to work; here's a general summary:

let xCounter := 0;

let xTotalMatchingRecords := cnt(Table);

for i in range(0, xTotalMatchingRecords) do

xCount := xCount + 1;

xCount + item(Table.Field, xCount)

end

This gives me what I'm expecting, but only 1 of the matching results (because the item function starts at 0, not 1, when looking for matching results).  

However, if I increment the counter after printing the result in the loop I get nothing, which makes no sense to me as the variable already was declared at the beginning.  

On the other hand, if I type "xCount + item(Table.Field, xCount -1)" I get what I'd expect.

Why do I have to increment the counter before I print the result, when I still have to subtract 1 from the counter when printing using the item function?  Why can't I increment the counter after the item function?

 

Greg

6 replies

null
    • Ninox partner
    • RoSoft_Steven.1
    • 3 yrs ago
    • Reported - view

    The last time your loop is run you add 1 more to the counter XCount.

    Maybe using a select command would be more advised: cnt(select Table [conditions])

    Is this the whole set of your code we see above? Not sure I understand wat your goal is here.

    let xCounter := 0; >>> let xCount := 0;

    cnt(Table)  >>> Is Table a field? If it is a table I would use cnt(select Table)

    the variable i in the loop isn't used (he goes from 0 to XTotalMatchingRecords) and is already automatically increased with one, so why using xCount..... etc....

    Maybe give us a litlle more details and the goal you want to achieve, it would help us more to give you advise.

    Steven

    • Greg_Prill
    • 3 yrs ago
    • Reported - view

    Hello, Steven:

    I mis-typed above: xCount is the same as xCounter, I just forgot to type "er" at the end of xCount.  I'm trying to figure out the syntax that's to be used in Ninox, and the manual only gives hints as to what will work, but never why it'll work.

     

    The usuer manual provides no real world context for syntax.  When using the "item" function, the second portion is the relevant matching record number, with the related index starting at zero.  The online manual suggests the following when using the item function (assuming there are only 3 sequential records in the table):

     

    Record ID.       Position Number.

    1                0

    2                1

    3                2

     

    To me, this means that when I type "item(Table.Field, <variable>), where <variable> = 0, I should get the record that matches Record ID "1".  If I've declared <variable> to be equal to zero prior to the loop starting, I'd expect that "item(Table.Fleld, <variable>)" to be the equivilent of typing "item(Table.Field, 0)", whether or not I've incremented <variable>.  Instead, nothing is returned unless I increment <variable> before invoking the "item" function.  

     

    In other words, the following works:

    <prior code, with let xCounter := 0 already declared>

    xCounter := xCounter + 1;

    item(Table.Field, xCounter - 1) {I have to undo the increment to get the result to start with the 1st record, and not the second.}

    end

     

    But this doesn't work:

    <prior code, with let xCounter := 0 already declared>

    item(Table.Field, xCounter);

    xCounter := xCounter + 1

    end

     

    I can see no reason why the first example works, but the second doesn't work.

     

    Greg

    • Ninox partner
    • RoSoft_Steven.1
    • 3 yrs ago
    • Reported - view

    Strange, the second should also work I think....

    If you try:

    -----------

    for i in range(0, xTotalMatchingRecords) do

    item(Table.Field, i)

    end

    -----------

    does this work?

    And by the way, its not always record with internal ID n°1 on the first place, you should see it as an array and depends how you've let them order. Also when you delete record n°1 then it's record with internal ID 2 in the first place. e.g.: if I use 'select Table order by TxtField' i get this:

    Record ID.      TxtField(Ordered)V    Position Number
    11               a                      0
    3                b                      1
    4                c                      2
    2                d                      3

    Another example with a while... do loop:

    let i := 0;
    while i != cnt(select Table order by Field) do
    item(Table.Field,i);

    i := i + 1
    end

    You can find the most recent manual here : https://docs.ninox.com/en/

    (ps: If you click on the link and get an error, you have to copy the text of the link instead.)

    Steven

    • John_Halls
    • 3 yrs ago
    • Reported - view

    Hi Greg

     

    The item() function works with arrays and I don't think Table.Field is an array.

     

    I'd answer Stephen's questions, especially what is it you are trying to do?

     

    Regards John

    • Fred
    • 3 yrs ago
    • Reported - view

    Hi Greg -

    To build on Steven's great suggestions. The item function takes an array then pulls the value from the position that you set after the comma.

     

    For example, if my array is (1,2,4,5) and I do item(array,2) I would get 4. Since Ninox starts counting at 0, 2 would be the third item in the array. item has no relationship to any record Id unless your array does.

    • Greg_Prill
    • 3 yrs ago
    • Reported - view

    Hi, guys:

     

    Thank you, Steven.  The new manual layout looks delightful, and I look forward to more items being added over time.  Also, thank you for confirming that either configuration of the sample code snippet should work.  Since that's true, then there's another factor that I didn't type out in my original post that's causing the problem.

     

    John: Thank you for your suggestion, but I think that Steven responded in a manner that explained how things are supposed to work, and is as confused as I am at present on why the second code snippet doesn't work.  I'm not a code ninja by any stretch, but there's no earthly reason why the first works, while the second doesn't (though I'll note that most of the helpful suggestions I've seen on the forums show the increment typed before invoking the function.).

     

    Fred, understood.  I think I understand how the function is supposed to work (thank you, Steven, for confirming), my problem arises from it not doing what both Steven and I think should happen when typed in either configuration.  I understand how item is supposed to work, my problem is that it doesn't work the way it's supposed to work on my end of things.  There's no reason why an instantiated variable of "0" shouldn't be recognized as such by the system.

Content aside

  • 3 yrs agoLast active
  • 6Replies
  • 790Views