0

Universal array handling when array items are not text strings

There are at least a couple of threads proposing user-defined functions to make array handling more convenient - but they all work only for arrays of text strings, since they use split/concat to do their work.

I would really like to manage arrays of variable length that contain, for example, record references.

I can do this with record ids (converted to text representations of numbers), but then each time I want to access a record, I have to call the record(table,recnum) function to retrieve the record. This is OK with fairly small array sizes, but I anticipate it could be a performance problem with big tables.

I can also do this (somewhat) by predefining the array of a largest possible length, but then I have to predict the largest array size I'll ever need (cause I'll forget if I try to exceed it and don't update the code first).

Any ideas on how to do this? Or this is a request for new Ninox array capabilities?

What I'd really like is built-in functions for array item appending, inserting, deleting, etc. regardless of the kind of data stored in the array.

7 replies

null
    • Sean
    • 5 yrs ago
    • Reported - view

    I don't speak for the Ninox team, but I am interested in what you consider a large array and whether you have tested the performance on that size of an array. I don't know why you think they would put memory management in the hands of their users... This isn't C.

     

    I ran this code in the console on my 2015 MacBook Pro, not exactly state of the art, and it was remarkably fast.

     

    let a1 := [""];

    for i in range(0, 2000) do

    aAppend(concat(a1), "Test " + i)

    end;

    a1

    • Sean
    • 5 yrs ago
    • Reported - view

    Sorry, it should be

     

    a1 := aAppend(concat(a1), "Test " + i)

    • Tim
    • 5 yrs ago
    • Reported - view

    Since I'm looking for how to manipulate arrays that don't contain text values, I keep looking at the slice function, because it does return a new array, regardless of array element type, but there doesn't appear to be any way to combine a slice with anything else.

    Something like this to delete the 3rd array element:

    var a := [1, 2, 3, 4];

    a := slice(a, 0, 2) + 5;

    a would be [1, 2, 5] after this. 

    Is there no way to combine slices into a new array?

    • Sean
    • 5 yrs ago
    • Reported - view

    There isn’t a built-in function or capability that i’m aware of. So, you’re left with trying to grow your own or begging for an enhancement to the Ninox language. I’m pretty sure this isn’t a popular request so I wouldn’t put too much hope in the language enhancement.

    • Gunther.1
    • 5 yrs ago
    • Reported - view

    Hi, try this:

    let array := [0, 20, 30, 40];
    let max := cnt(array) + 1;
    let add := 75;
    var newArray := for ind from 0 to max do
    if ind = max - 1 then add else item(array, ind) end
    end;
    alert(text(newArray))

    • Tim
    • 5 yrs ago
    • Reported - view

    Oh, yes, baby! That's it! I knew there was something in there somewhere that would do it. Thank you, Günther!

    This will work great. I may have a lot of records, but the stack won't ever go more than 10 or so deep (I just don't want to have to predict it).

    Here's the same code storing record references instead of numbers:

    let array := [record(Entries,1).Id, record(Entries,2).Id, record(Entries,3).Id];
    let max := cnt(array) + 1;
    let add := record(Entries,4).Id;
    var newArray := for ind from 0 to max do
    if ind = max - 1 then add else item(array, ind) end
    end;
    alert(text(newArray.Headline))

    • Sean
    • 5 yrs ago
    • Reported - view

    Why not? If you want to insert...

     

    let array := [0, 20, 30, 40];
    let max := cnt(array) + 1;
    let iLoc := 2;
    let iItem := 75;
    var newArray := for idx from 0 to max do
    if idx < iLoc then
    item(array, idx)
    else
    if idx = iLoc then iItem else item(array, idx - 1) end
    end
    end;
    alert(text(newArray))

Content aside

  • 5 yrs agoLast active
  • 7Replies
  • 3186Views
  • 1 Following