0

Some User Defined Array Functions

aAppend() - Can fill an empty array and adds items to the end of the array.
aInsert() - Inserts a new item anywhere in the array except the end.
aMerge() - Merges two separate arrays.
aRemove() - Removes an array item.
aReplace() - Replaces an item in an array with a new item.
aSwap() - Swaps two items in an array.

Function Calls

aAppend(concat(yourArray), "newItem")
aInsert(concat(yourArray), "aryItem", "newItem")
aMerge(concat(yourArray1), concat(yourArray2)
aRemove(concat(yourArray), "aryItem")
aReplace(concat(yourArray), "aryItem", "newItem")
aSwap(concat(yourArray), "aryItem1", "aryItem2")

Function Definitions

function aAppend(ary : text,newItem : text) do
if count(ary) > 0 then
split(replace(ary, ", ", ",") + "," + newItem, ",")
else
split(newItem, ",")
end
end;

function aInsert(ary : text,insLoc : text,insItem : text) do
if count(split(ary, ",")) > 0 then
if contains(ary, insLoc) then
split(replace(replace(ary, insLoc, insItem + "," + insLoc), ", ", ","), ",")
else
alert("aInsert( ) :  " + insLoc + " is not in the array!");
split(replace(ary, ", ", ","), ",")
end
else
alert("aInsert( ) :  Can't insert into an empty array!  Use aAppend( )")
end
end;

function aMerge(ary1 : text,ary2 : text) do
split(replace(ary1 + "," + ary2, ", ", ","), ",")
end;

function aRemove(ary : text,delItem : text) do
if count(split(ary, ",")) > 0 then
if contains(ary, delItem) then
if index(ary, delItem) + length(delItem) != length(ary) then
split(replace(replace(ary, delItem + ", ", ""), ", ", ","), ",")
else
split(replace(replace(ary, delItem, ""), ", ", ","), ",")
end
else
alert("aRemove( ) :  " + delItem + " is not in the array!");
split(replace(ary, ", ", ","), ",")
end
else
alert("aRemove( ) :  The array is empty!")
end
end;

function aReplace(ary : text,fromItem : text,toItem : text) do
if contains(ary, fromItem) then
split(replace(replace(ary, fromItem, toItem), ", ", ","), ",")
else
alert("aReplace( ) :  " + fromItem + " is not in the array!");
split(replace(ary, ", ", ","), ",")
end
end;

function aSwap(ary : text,itmOne : text,itmTwo : text) do
let myString := "";
if count(split(ary, ",")) > 2 then
if contains(ary, itmOne) then
if contains(ary, itmTwo) then
let itmTmp := "#%$";
ary := replace(ary, itmOne, itmTmp);
ary := replace(ary, itmTwo, itmOne);
ary := replace(ary, itmTmp, itmTwo);
split(replace(ary, ", ", ","), ",")
else
alert("aSwap( ) :  " + itmTwo + " is not in the array!");
split(replace(ary, ", ", ","), ",")
end
else
alert("aSwap( ) :  " + itmOne + " is not in the array!");
split(replace(ary, ", ", ","), ",")
end
else
alert("aSwap( ) :  There must be 2 or more items in the array!");
split(replace(ary, ", ", ","), ",")
end
end

23 replies

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

    Thanks for sharing

    • Sean
    • 5 yrs ago
    • Reported - view

    You bet. If you or anyone make a mod or have a suggestion, please post it.

    • Sean
    • 5 yrs ago
    • Reported - view

    Something I found very helpful is that functions defined in "Global scrip definitions" can be called in the console.

    • blackie
    • 5 yrs ago
    • Reported - view

    Shouldn't the 'if' statement in aAppend be similar to the other functions?

    count() is listed as a array fucntions, but ary is text.

     

    function aAppend(ary : text,newItem : text) do
    if count(ary) > 0 then

     

    shouldn't it be

     

    if count(split(ary, ",")) > 0 then

    • Sean
    • 5 yrs ago
    • Reported - view

    Blackie, for the sake of consistency yes. But, all it’s checking for is whether the string is empty or not. The result will be either 0 or 1.

     

    Looks like I’ll be out of commission for a while. The result of a sequence of events that ended with my laptop falling to the floor.

    • Sean
    • 5 yrs ago
    • Reported - view

    An external monitor will have to do for now... I reposted all of the functions because it's a pain to compare what's changed. There is still quite a bit of error checking and validating that can be done. This update addresses blackie's question, returns an unaltered array when there is an error and changes the count test in aSwap from > 2 to >= 2.

    function aAppend(ary : text,newItem : text) do
    if length(newItem) != 0 then
    if count(split(ary, ",")) > 0 then
    split(replace(ary, ", ", ",") + "," + newItem, ",")
    else
    split(newItem, ",")
    end
    else
    alert("aAppend( ) :  New array item can't be empty!");
    split(replace(ary, ", ", ","), ",")
    end
    end;

    function aInsert(ary : text,insLoc : text,insItem : text) do
    if count(split(ary, ",")) > 0 then
    if contains(ary, insLoc) then
    split(replace(replace(ary, insLoc, insItem + "," + insLoc), ", ", ","), ",")
    else
    alert("aInsert( ) :  " + insLoc + " is not in the array!");
    split(replace(ary, ", ", ","), ",")
    end
    else
    alert("aInsert( ) :  Can't insert into an empty array!  Use aAppend( )");
    split(replace(ary, ", ", ","), ",")
    end
    end;

    function aMerge(ary1 : text,ary2 : text) do
    split(replace(ary1 + "," + ary2, ", ", ","), ",")
    end;

    function aRemove(ary : text,delItem : text) do
    if count(split(ary, ",")) > 0 then
    if contains(ary, delItem) then
    if index(ary, delItem) + length(delItem) != length(ary) then
    split(replace(replace(ary, delItem + ", ", ""), ", ", ","), ",")
    else
    split(replace(replace(ary, delItem, ""), ", ", ","), ",")
    end
    else
    alert("aRemove( ) :  " + delItem + " is not in the array!");
    split(replace(ary, ", ", ","), ",")
    end
    else
    alert("aRemove( ) :  The array is empty!");
    split(replace(ary, ", ", ","), ",")
    end
    end;

    function aReplace(ary : text,fromItem : text,toItem : text) do
    if contains(ary, fromItem) then
    split(replace(replace(ary, fromItem, toItem), ", ", ","), ",")
    else
    alert("aReplace( ) :  " + fromItem + " is not in the array!");
    split(replace(ary, ", ", ","), ",")
    end
    end;

    function aSwap(ary : text,itmOne : text,itmTwo : text) do
    if count(split(ary, ",")) >= 2 then
    if contains(ary, itmOne) then
    if contains(ary, itmTwo) then
    let itmTmp := "#%$";
    ary := replace(ary, itmOne, itmTmp);
    ary := replace(ary, itmTwo, itmOne);
    ary := replace(ary, itmTmp, itmTwo);
    split(replace(ary, ", ", ","), ",")
    else
    alert("aSwap( ) :  " + itmTwo + " is not in the array!");
    split(replace(ary, ", ", ","), ",")
    end
    else
    alert("aSwap( ) :  " + itmOne + " is not in the array!");
    split(replace(ary, ", ", ","), ",")
    end
    else
    alert("aSwap( ) :  There must be 2 or more items in the array!");
    split(replace(ary, ", ", ","), ",")
    end
    end

    • Sean
    • 5 yrs ago
    • Reported - view

    Interesting behavior... The issue in this thread, https://ninoxdb.de/en/forum/technical-help-5ab8fe445fe2b42b7dd39ee7/this-is-driving-me-crazy-5c1c7d62cf005e2aa074793e, isn't a problem if the first function in the "Global script definitions" is called by another function in there.

     

    Three new functions...

     

    aIsEmpty() - Tests for an empty array and is used by some of the other functions.

    aClear() - Returns an empty array.

    aGetIndex() - Returns the index number of the passed array item.

     

    Function Calls

     

    aIsEmpty(concat(yourArray))

    aClear(concat(yourArray))

    aGetIndex(concat(yourArray), "aryItem")

     

    Function Definitions

     

    I've updated most of the function definitions for cleanup and better validation.

     

    function aIsEmpty(ary : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if count(myArray) > 0 then false else true end
    end;

    function aAppend(ary : text,newItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if length(newItem) != 0 then
    if not aIsEmpty(ary) then
    myArray := split(replace(ary, ", ", ",") + "," + newItem, ",")
    else
    myArray := split(newItem, ",")
    end
    else
    alert("aAppend( ) :  New array item can't be empty!")
    end;
    myArray
    end;

    function aClear(ary : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    ary := "";
    myArray := split(ary, ",")
    end;
    myArray
    end;

    function aGetIndex(ary : text,aryItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    let aryIndex := 0;
    while aryItem != item(myArray, aryIndex) and aryIndex < count(myArray) do 
    aryIndex := aryIndex + 1
    end
    ;
    if aryIndex < count(myArray) then
    aryIndex
    else
    alert("aGetIndex( ) :  """ + aryItem + """ is not in the array!");
    -1
    end
    end;

    function aInsert(ary : text,insLoc : text,insItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    let aryIndex := 0;
    while insLoc != item(myArray, aryIndex) and aryIndex < count(myArray) do 
    aryIndex := aryIndex + 1
    end
    ;
    if aryIndex < count(myArray) then
    myArray := split(replace(replace(ary, insLoc, insItem + "," + insLoc), ", ", ","), ",")
    else
    alert("aInsert( ) :  """ + insLoc + """ is not in the array!")
    end
    else
    alert("aInsert( ) :  Can't insert into an empty array!  Use aAppend( )")
    end;
    myArray
    end;

    function aMerge(ary1 : text,ary2 : text) do
    let myArray := split(replace(ary1 + "," + ary2, ", ", ","), ",");
    myArray
    end;

    function aRemove(ary : text,delItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    let aryIndex := 0;
    while delItem != item(myArray, aryIndex) and aryIndex < count(myArray) do 
    aryIndex := aryIndex + 1
    end
    ;
    if aryIndex < count(myArray) then
    if index(ary, delItem) + length(delItem) != length(ary) then
    myArray := split(replace(replace(ary, delItem + ", ", ""), ", ", ","), ",")
    else
    myArray := split(replace(replace(ary, delItem, ""), ", ", ","), ",")
    end
    else
    alert("aRemove( ) :  """ + delItem + """ is not in the array!")
    end
    else
    alert("aRemove( ) :  The array is empty!")
    end;
    myArray
    end;

    function aReplace(ary : text,fromItem : text,toItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    let aryIndex := 0;
    while fromItem != item(myArray, aryIndex) and aryIndex < count(myArray) do 
    aryIndex := aryIndex + 1
    end
    ;
    if aryIndex < count(myArray) then
    myArray := split(replace(replace(ary, fromItem, toItem), ", ", ","), ",")
    else
    alert("aReplace( ) :  """ + fromItem + """ is not in the array!")
    end
    else
    alert("aReplace( ) :  The array is empty!")
    end;
    myArray
    end;

    function aSwap(ary : text,itmOne : text,itmTwo : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    if count(myArray) >= 2 then
    let aryIndex1 := 0;
    while itmOne != item(myArray, aryIndex1) and aryIndex1 < count(myArray) do 
    aryIndex1 := aryIndex1 + 1
    end
    ;
    if aryIndex1 < count(myArray) then
    let aryIndex2 := 0;
    while itmTwo != item(myArray, aryIndex2) and aryIndex2 < count(myArray) do 
    aryIndex2 := aryIndex2 + 1
    end
    ;
    if aryIndex2 < count(myArray) then
    let itmTmp := "#%$";
    ary := replace(ary, itmOne, itmTmp);
    ary := replace(ary, itmTwo, itmOne);
    ary := replace(ary, itmTmp, itmTwo);
    myArray := split(replace(ary, ", ", ","), ",")
    else
    alert("aSwap( ) :  """ + itmTwo + """ is not in the array!")
    end
    else
    alert("aSwap( ) :  """ + itmOne + """ is not in the array!")
    end
    else
    alert("aSwap( ) :  There must be 2 or more items in the array!")
    end
    else
    alert("aSwap( ) :  The array is empty!")
    end;
    myArray
    end

    • Sean
    • 5 yrs ago
    • Reported - view

    I missed something obvious last night. aGetIndex() can be used to replace the while loops in aInsert(), aRemove(), aReplace() and aSwap(). Also, aGetIndex() and aIsEmpty() should be defined before any of the other UDF array functions.

     

    function aGetIndex(ary : text,aryItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    let aryIndex := 0;
    while aryItem != item(myArray, aryIndex) and aryIndex < count(myArray) do 
    aryIndex := aryIndex + 1
    end
    ;
    aryIndex
    end;

     

    function aInsert(ary : text,insLoc : text,insItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    let aryIndex := aGetIndex(ary, insLoc);
    if aryIndex < count(myArray) then
    myArray := split(replace(replace(ary, insLoc, insItem + "," + insLoc), ", ", ","), ",")
    else
    alert("aInsert( ) :  """ + insLoc + """ is not in the array!")
    end
    else
    alert("aInsert( ) :  Can't insert into an empty array!  Use aAppend( )")
    end;
    myArray
    end;

    function aRemove(ary : text,delItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    let aryIndex := aGetIndex(ary, delItem);
    if aryIndex < count(myArray) then
    if index(ary, delItem) + length(delItem) != length(ary) then
    myArray := split(replace(replace(ary, delItem + ", ", ""), ", ", ","), ",")
    else
    myArray := split(replace(replace(ary, delItem, ""), ", ", ","), ",")
    end
    else
    alert("aRemove( ) :  """ + delItem + """ is not in the array!")
    end
    else
    alert("aRemove( ) :  The array is empty!")
    end;
    myArray
    end;

    function aReplace(ary : text,fromItem : text,toItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    let aryIndex := aGetIndex(ary, fromItem);
    if aryIndex < count(myArray) then
    myArray := split(replace(replace(ary, fromItem, toItem), ", ", ","), ",")
    else
    alert("aReplace( ) :  """ + fromItem + """ is not in the array!")
    end
    else
    alert("aReplace( ) :  The array is empty!")
    end;
    myArray
    end;

    function aSwap(ary : text,itmOne : text,itmTwo : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    if count(myArray) >= 2 then
    let aryIndex1 := aGetIndex(ary, itmOne);
    if aryIndex1 < count(myArray) then
    let aryIndex2 := aGetIndex(ary, itmTwo);
    if aryIndex2 < count(myArray) then
    let itmTmp := "#%$";
    ary := replace(ary, itmOne, itmTmp);
    ary := replace(ary, itmTwo, itmOne);
    ary := replace(ary, itmTmp, itmTwo);
    myArray := split(replace(ary, ", ", ","), ",")
    else
    alert("aSwap( ) :  """ + itmTwo + """ is not in the array!")
    end
    else
    alert("aSwap( ) :  """ + itmOne + """ is not in the array!")
    end
    else
    alert("aSwap( ) :  There must be 2 or more items in the array!")
    end
    else
    alert("aSwap( ) :  The array is empty!")
    end;
    myArray
    end

    • Sean
    • 5 yrs ago
    • Reported - view

    One more small adjustment. aInsert() can now be used append to an empty array or existing array. Call the function like this...

     

    aInsert(concat(yourArray), "", "newItem")

     

    function aInsert(ary : text,insLoc : text,insItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if insLoc != "" then
    let aryIndex := aGetIndex(ary, insLoc);
    if aryIndex < count(myArray) then
    myArray := split(replace(replace(ary, insLoc, insItem + "," + insLoc), ", ", ","), ",")
    else
    alert("aInsert( ) :  """ + insLoc + """ is not in the array!")
    end
    else
    myArray := aAppend(ary, insItem)
    end;
    myArray
    end;

     

    It simply calls the aAppend() function instead of giving the error message.

    • Sean
    • 5 yrs ago
    • Reported - view

    This code fixes string replace errors in aReplace() and aSwap(). For those who are interested, these functions are, for the most part, manipulating strings to simulate array functions that are available in other programming languages. The problem fixed here is one where an item being replaced or swapped is a substring of more than one items.

     

    For example, if you replace "Test 1" with "Hello" in an array with "Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Test 6", "Test 7", "Test 8", "Test 9", "Test 10", "Test 11" the items "Test 10" and "Test 11" would be changed to "Hello0" and "Hello1".

     

    function aReplace(ary : text,fromItem : text,toItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    let aryIndex := aGetIndex(ary, fromItem);
    if aryIndex < count(myArray) then
    if index(ary, fromItem) + length(fromItem) != length(ary) then
    myArray := split(replace(replace(ary, fromItem + ", ", toItem + ", "), ", ", ","), ",")
    else
    myArray := split(replace(replace(ary, fromItem, toItem), ", ", ","), ",")
    end
    else
    alert("aReplace( ) :  """ + fromItem + """ is not in the array!")
    end
    else
    alert("aReplace( ) :  The array is empty!")
    end;
    myArray
    end;

    function aSwap(ary : text,itmOne : text,itmTwo : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    if count(myArray) >= 2 then
    let aryIndex1 := aGetIndex(ary, itmOne);
    if aryIndex1 < count(myArray) then
    let aryIndex2 := aGetIndex(ary, itmTwo);
    if aryIndex2 < count(myArray) then
    let itmTmp := "#%$";
    if index(ary, itmOne) + length(itmOne) != length(ary) then
    ary := replace(ary, itmOne + ", ", itmTmp + ", ")
    else
    ary := replace(ary, itmOne, itmTmp)
    end;
    if index(ary, itmTwo) + length(itmTwo) != length(ary) then
    ary := replace(ary, itmTwo + ", ", itmOne + ", ")
    else
    ary := replace(ary, itmTwo, itmOne)
    end;
    ary := replace(ary, itmTmp, itmTwo);
    myArray := split(replace(ary, ", ", ","), ",")
    else
    alert("aSwap( ) :  """ + itmTwo + """ is not in the array!")
    end
    else
    alert("aSwap( ) :  """ + itmOne + """ is not in the array!")
    end
    else
    alert("aSwap( ) :  There must be 2 or more items in the array!")
    end
    else
    alert("aSwap( ) :  The array is empty!")
    end;
    myArray
    end

    • Sean
    • 5 yrs ago
    • Reported - view

    aMove() - Moves an array item to the top, bottom or between items of the array. It must be defined after aGetIndex(), aIsEmpty(), aAppend(), aInsert() and aRemove() to work as it is defined here.

     

    Function Calls

     

    aMove(concat(yourArray), "", "moveItem") - Moves the item to the end of the array.

    aMove(concat(yourArray), "aryItem", "moveItem") - Moves the item anywhere else in the array.

     

    Function Definition

     

    function aMove(ary : text,movLoc : text,movItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    if movLoc != "" then
    let aryIndex := aGetIndex(ary, movLoc);
    if aryIndex < count(myArray) then
    myArray := aRemove(ary, movItem);
    myArray := aInsert(concat(myArray), movLoc, movItem)
    else
    alert("aMove( ) :  """ + movLoc + """ is not in the array!")
    end
    else
    myArray := aRemove(ary, movItem);
    myArray := aAppend(concat(myArray), movItem)
    end
    else
    alert("aMove( ) :  The array is empty!")
    end;
    myArray
    end;

    • Sean
    • 5 yrs ago
    • Reported - view

    aContains() - Returns a boolean value. This replaces aGetIndex() in UDF's aInsert(), aRemove(), aMove(), aReplace() and aSwap(). Also, the aMove() function definition is updated to validate movItem.

     

    Function Call

     

    aContains(concat(yourArray), "aryItem")

     

    Function Definitions

     

    function aContains(ary : text,aryItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    let aryIndex := 0;
    while aryItem != item(myArray, aryIndex) and aryIndex < count(myArray) do 
    aryIndex := aryIndex + 1
    end
    ;
    if aryIndex < count(myArray) then
    true
    else
    false
    end
    end;

    function aGetIndex(ary : text,aryItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    let aryIndex := 0;
    while aryItem != item(myArray, aryIndex) and aryIndex < count(myArray) do 
    aryIndex := aryIndex + 1
    end
    ;
    aryIndex
    end;

    function aIsEmpty(ary : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if count(myArray) > 0 then false else true end
    end;

    function aAppend(ary : text,newItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if length(newItem) != 0 then
    if not aIsEmpty(ary) then
    myArray := split(replace(ary, ", ", ",") + "," + newItem, ",")
    else
    myArray := split(newItem, ",")
    end
    else
    alert("aAppend( ) :  New array item can't be empty!")
    end;
    myArray
    end;

    function aClear(ary : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    ary := "";
    myArray := split(ary, ",")
    end;
    myArray
    end;

    function aInsert(ary : text,insLoc : text,insItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if insLoc != "" then
    if aContains(ary, insLoc) then
    myArray := split(replace(replace(ary, insLoc, insItem + "," + insLoc), ", ", ","), ",")
    else
    alert("aInsert( ) :  """ + insLoc + """ is not in the array!")
    end
    else
    myArray := aAppend(ary, insItem)
    end;
    myArray
    end;

    function aMerge(ary1 : text,ary2 : text) do
    let myArray := split(replace(ary1 + "," + ary2, ", ", ","), ",");
    myArray
    end;

    function aRemove(ary : text,delItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    if aContains(ary, delItem) then
    if index(ary, delItem) + length(delItem) != length(ary) then
    myArray := split(replace(replace(ary, delItem + ", ", ""), ", ", ","), ",")
    else
    myArray := split(replace(replace(ary, delItem, ""), ", ", ","), ",")
    end
    else
    alert("aRemove( ) :  """ + delItem + """ is not in the array!")
    end
    else
    alert("aRemove( ) :  The array is empty!")
    end;
    myArray
    end;

    function aMove(ary : text,movLoc : text,movItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    if movLoc != "" then
    if aContains(ary, movLoc) then
    if aContains(ary, movItem) then
    myArray := aRemove(ary, movItem);
    myArray := aInsert(concat(myArray), movLoc, movItem)
    else
    alert("aMove( ) :  """ + movItem + """ is not in the array!")
    end
    else
    alert("aMove( ) :  """ + movLoc + """ is not in the array!")
    end
    else
    if aContains(ary, movItem) then
    myArray := aRemove(ary, movItem);
    myArray := aAppend(concat(myArray), movItem)
    else
    alert("aMove( ) :  """ + movItem + """ is not in the array!")
    end
    end
    else
    alert("aMove( ) :  The array is empty!")
    end;
    myArray
    end;

    function aReplace(ary : text,frmItem : text,toItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    if aContains(ary, frmItem) then
    if index(ary, frmItem) + length(frmItem) != length(ary) then
    myArray := split(replace(replace(ary, frmItem + ", ", toItem + ", "), ", ", ","), ",")
    else
    myArray := split(replace(replace(ary, frmItem, toItem), ", ", ","), ",")
    end
    else
    alert("aReplace( ) :  """ + frmItem + """ is not in the array!")
    end
    else
    alert("aReplace( ) :  The array is empty!")
    end;
    myArray
    end;

    function aSwap(ary : text,itmOne : text,itmTwo : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if not aIsEmpty(ary) then
    if count(myArray) >= 2 then
    if aContains(ary, itmOne) then
    if aContains(ary, itmTwo) then
    let itmTmp := "#%$";
    if index(ary, itmOne) + length(itmOne) != length(ary) then
    ary := replace(ary, itmOne + ", ", itmTmp + ", ")
    else
    ary := replace(ary, itmOne, itmTmp)
    end;
    if index(ary, itmTwo) + length(itmTwo) != length(ary) then
    ary := replace(ary, itmTwo + ", ", itmOne + ", ")
    else
    ary := replace(ary, itmTwo, itmOne)
    end;
    ary := replace(ary, itmTmp, itmTwo);
    myArray := split(replace(ary, ", ", ","), ",")
    else
    alert("aSwap( ) :  """ + itmTwo + """ is not in the array!")
    end
    else
    alert("aSwap( ) :  """ + itmOne + """ is not in the array!")
    end
    else
    alert("aSwap( ) :  There must be 2 or more items in the array!")
    end
    else
    alert("aSwap( ) :  The array is empty!")
    end;
    myArray
    end

    • Sean
    • 5 yrs ago
    • Reported - view

    It's not easy being the only debugger 🤪

    A fix for aInsert() and another version of aContains(). If you want to see the error for aInsert() you can paste this in the console...

     

    let a1 := [""];
    for i in range(1, 21) do
    a1 := aAppend(concat(a1), "Test " + i)
    end;
    a1 := aInsert(concat(a1), "Test 1", "Hello");
    a1

     

    function aInsert(ary : text,insLoc : text,insItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if insLoc != "" then
    if aContains(ary, insLoc) then
    myArray := split(replace(replace(ary, insLoc + ",", insItem + "," + insLoc + ","), ", ", ","), ",")
    else
    alert("aInsert( ) :  """ + insLoc + """ is not in the array!")
    end
    else
    myArray := aAppend(ary, insItem)
    end;
    myArray
    end;

     

    This version of aContains() calls aGetIndex() so aGetIndex() must be defined before aContains() for this to work.

     

    function aContains(ary : text,aryItem : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if aGetIndex(ary, aryItem) < count(myArray) then
    true
    else
    false
    end
    end;

    • Sean
    • 5 yrs ago
    • Reported - view

    aGetItem() - This function returns the array item of a given index.

     

    Function Call

     

    aGetItem(concat(yourArray), aryIndex)

     

    Function Definition

     

    function aGetItem(ary : text,aryIndex : number) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if aryIndex >= 0 then
    if aryIndex < count(myArray) then
    item(myArray, aryIndex)
    else
    alert("aGetItem( ) :  Index is out of range! Array length is " + count(myArray) + ".")
    end
    else
    alert("aGetItem( ) :  Index can't be less than 0!")
    end
    end;

    • Sean
    • 5 yrs ago
    • Reported - view

    Ooops, brainfart! item(array, index) is already built-in. Sorry!

    • dessein
    • 5 yrs ago
    • Reported - view

    Hi Sean,

    Great array function! Thanks! I’m using (the latest versions of) acontains and agetindex. If you call agetindex with concat(Myarray) as argument, it works ok!

    If you call agetindex with Array1 as argument where you use let Array1 := concat(Myarray), then you always get 0 as return, independent of the index position in the array, which is wrong!

    so

    agetindex(concat(Myarray), Myitem) works!

    But

    Let Array1:= concat(Myarray);

    agetindex(Array1, Myitem) doesnn’t!

    i can’t figure out why behaviour is different.

    Of course in both situations Myitem is present in the array, and not always on index position 0!!!

    • Sean
    • 5 yrs ago
    • Reported - view

    Hi dessein,

    Thank you! I tested the function using a "preconcatenated" variable in the function and it worked. One thing I do see is you have "Let" instead of "let". Ninox language is case sensitive so that will make a difference. If that was just a typo in your post let me know.

    • dessein
    • 5 yrs ago
    • Reported - view

    Let is indeed a typo in my post.

    i use let in my code. The only thing i can think of, the array is made up of “choice” type of items coming from a ninox table field Task.

    code with issues:

    let aTask := concat(Action.Task);
    if aContains(aTask, "Sow") then
     aGetIndex(aTask, "Sow")      => always returns 0
    else
    null
    end

    code without issues:

    if aContains(concat(Action.Task), "Sow") then
     aGetIndex(concat(Action.Task), "Sow")     => returns the correct index!!!
    else
    null
    end

    it took we long time to find this “workaround...

    • Sean
    • 5 yrs ago
    • Reported - view

    Did you mean "Multiple choice" instead of "choice" type? I did some testing with the "Multiple choice" type and got the same result you did. It looks like the user-defined functions do not allow user variables as arguments. Even if you used (concat(Action.Task), but replaced "Sow" with a variable that contained "Sow" it would not work correctly. In this case I believe it would return an index 1 greater than the index of the last item in the array.

    • dessein
    • 5 yrs ago
    • Reported - view

    No, “choice” .

    • Sean
    • 5 yrs ago
    • Reported - view

    I'm pretty sure the "Choice" field type does not return an array.

    • dessein
    • 5 yrs ago
    • Reported - view

    To clarify: i use a table Succulent with a subtable Action. When on a particular Succulent record, the command Action.Task doesn’t return an array with all Action records? Altough concat(Action.Task) does give me a text string with all Action records with the Task field. E.g. Sow ,Repot , Transplant for that particular Succulent record.

    • Sean
    • 5 yrs ago
    • Reported - view

    I follow you now and, yes, you are correct. I thought you were using a "Choice" field type, but you are a linked table which would return an array of values.

Content aside

  • 5 yrs agoLast active
  • 23Replies
  • 8359Views