2

UD Array Functions by Index

This has definitely been a progression. Typically, array manipulation is done by using the array's index rather than by the index value and that's how these functions work.

 

Helper Functions

ahfChkIndex()

ahfIsEmpty()

Array Functions

aAppend() - Appends an item to an array. I didn't change the prefix because it's the same.

idxInsert() - Inserts an item to an array. If the index is out-of-range, it appends the new item.

idxRemove() - Removes the item at the given index.

idxMove() - Moves an item by index to a given index. If the index is out-of-range, it moves the item to the end.

idxReplace() - Replaces an item at a given index with a new item.

idxSwap() - Swaps two array items by the given indexes.

 

Function Calls

aAppend(concat(yourArray), aryItem)

idxInsert(concat(yourArray), aryIndex, aryItem)

idxRemove(concat(yourArray), aryIndex)

idxMove(concat(yourArray), frmIndex, toIndex)

idxReplace(concat(yourArray), aryIndex, newItem)

idxSwap(concat(yourArray), idxOne, idxTwo)

 

Function Definitions

 

function ahfChkIndex(ary : text,aryIndex : number) do
let myArray := split(replace(ary, ", ", ","), ",");
let idxValue := false;
if aryIndex < count(myArray) then
idxValue := true
end;
idxValue
end;

function ahfIsEmpty(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 ahfIsEmpty(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 idxInsert(ary : text,insIndex : number,insItem : text) do
let myArray := split(replace(ary, ", ", ","), ",");
let iI := abs(insIndex);
if ahfChkIndex(ary, iI) then
let insLoc := item(myArray, iI);
if iI != count(myArray) - 1 then
myArray := split(replace(replace(ary, insLoc + ",", insItem + "," + insLoc + ","), ", ", ","), ",")
else
myArray := split(replace(replace(ary, insLoc, insItem + "," + insLoc), ", ", ","), ",")
end
else
myArray := aAppend(ary, insItem)
end;
myArray
end;

function idxRemove(ary : text,rmvIndex : number) do
let myArray := split(replace(ary, ", ", ","), ",");
if not ahfIsEmpty(ary) then
let rI := abs(rmvIndex);
if ahfChkIndex(ary, rI) then
let rmvItem := item(myArray, rI);
if index(ary, rmvItem) + length(rmvItem) != length(ary) then
myArray := split(replace(replace(ary, rmvItem + ", ", ""), ", ", ","), ",")
else
myArray := split(replace(replace(ary, rmvItem, ""), ", ", ","), ",")
end
else
alert("idxRemove( ) :  Index [" + rI + "] is out of range [0.." + text(count(myArray) - 1) + "]")
end
else
alert("idxRemove( ) :  The array is empty!")
end;
myArray
end;

function idxMove(ary : text,movFromIndex : number,movToIndex : number) do
let myArray := split(replace(ary, ", ", ","), ",");
if not ahfIsEmpty(ary) then
let mFI := abs(movFromIndex);
let movItem := item(myArray, mFI);
if ahfChkIndex(ary, mFI) then
let mTI := abs(movToIndex);
if ahfChkIndex(ary, mTI) then
myArray := idxRemove(ary, mFI);
myArray := idxInsert(concat(myArray), mTI, movItem)
else
myArray := idxRemove(ary, mFI);
myArray := aAppend(concat(myArray), movItem)
end
else
alert("idxMove( ) :  Index [" + mFI + "] is out of range [0.." + text(count(myArray) - 1) + "]")
end
else
alert("idxMove( ) :  The array is empty!")
end;
myArray
end;

function idxReplace(ary : text,rplIndex : number,rplItem : text) do
let myArray := split(replace(ary, ", ", ","), ",");
if not ahfIsEmpty(ary) then
let rI := abs(rplIndex);
if ahfChkIndex(ary, rI) then
let oldItem := item(myArray, rI);
if index(ary, oldItem) + length(oldItem) != length(ary) then
myArray := split(replace(replace(ary, oldItem + ", ", rplItem + ", "), ", ", ","), ",")
else
myArray := split(replace(replace(ary, oldItem, rplItem), ", ", ","), ",")
end
else
alert("idxReplace( ) :  Index [" + rI + "] is out of range [0.." + text(count(myArray) - 1) + "]")
end
else
alert("idxReplace( ) :  The array is empty!")
end;
myArray
end;

function idxSwap(ary : text,idxOne : number,idxTwo : number) do
let myArray := split(replace(ary, ", ", ","), ",");
if not ahfIsEmpty(ary) then
if count(myArray) >= 2 then
let iO := abs(idxOne);
let itmOne := item(myArray, iO);
if ahfChkIndex(ary, iO) then
let iT := abs(idxTwo);
let itmTwo := item(myArray, iT);
if ahfChkIndex(ary, iT) 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("idxSwap( ) :  Index [" + iT + "] is out of range [0.." + text(count(myArray) - 1) + "]")
end
else
alert("idxSwap( ) :  Index [" + iO + "] is out of range [0.." + text(count(myArray) - 1) + "]")
end
else
alert("idxSwap( ) :  There must be 2 or more items in the array!")
end
else
alert("idxSwap( ) :  The array is empty!")
end;
myArray
end

 

If you find any errors please post them.

6 replies

null
    • Sean
    • 5 yrs ago
    • Reported - view

    idxContains() - Lets the user check for a value at a given index. Returns a boolean value.

     

    Function Call

     

    idxContains(concat(yourArray), aryIndex, chkValue)

     

    Function Definition

     

    function idxContains(ary : text,aryIndex : number,chkValue : text) do
    let myArray := split(replace(ary, ", ", ","), ",");
    if item(myArray, aryIndex) = chkValue then
    true
    else
    false
    end
    end

    • Sean
    • 5 yrs ago
    • Reported - view

    aryContains() - Lets the user check for a value in an array and returns the index. This function uses idxContains() so idxContains() must be defined before aryContains().

     

    Function Call

     

    aryContains(concat(yourArray), chkValue)

     

    Function Definition

     

    function aryContains(ary : text,chkValue : text) do
    let aryCount := count(split(replace(ary, ", ", ","), ","));
    let myIndex := 0;
    while myIndex < aryCount and not idxContains(ary, myIndex, chkValue) do 
    myIndex := myIndex + 1
    end;
    myIndex
    end;

     

    This is an example that can be run in the console if the above functions have been defined in your database...

     

    let a1 := [""];
    for i in range(1, 21) do
    a1 := aAppend(concat(a1), "Test " + i)
    end;
    let myIndex := aryContains(concat(a1), "Test 5");
    if myIndex < count(a1) then
    a1 := idxReplace(concat(a1), myIndex, "Hello")
    end;
    a1

    • ioannis.1
    • 4 yrs ago
    • Reported - view

    Hi I am trying to make a function to check if a string is a member in an array

    function aContains(txt : text,chk : text) do
    let ar := split(txt, ",");
    for i in range(0, cnt(ar)) do
    if item(ar, i) = chk then true else false end
    end
    end;

    I always get a true

    Can somebody help 

    Thanks

    • Sean
    • 4 yrs ago
    • Reported - view

    There are a couple of problems. As far as I'm able to tell, you still can't pass an array to a user-defined function so you would need to pass the array as a string using concat() or join(). Also, if you use a for-loop, the only time it would evaluate to true is if last item in the array matched the chk value, but that is only if you assign the result to a variable and return the variable.

     

    This works if you use join() on the array...

     

    function aContains(txt : text,chk : text) do
    let ar := split(txt, ",");
    let myChk := false;
    let lpCnt := 0;
    while lpCnt < cnt(ar) and not myChk do 
    if item(ar, lpCnt) = chk then
    myChk := true
    else
    lpCnt := lpCnt + 1
    end
    end;
    myChk
    end;

     

    Call it like this...

     

    aContains(join(yourArray, ","), chkValue)

     

    This works for concat()...

     

    function aContains(txt : text,chk : text) do
    let ar := split(replace(txt, ", ", ","), ",");
    let myChk := false;
    let lpCnt := 0;
    while lpCnt < cnt(ar) and not myChk do 
    if item(ar, lpCnt) = chk then
    myChk := true
    else
    lpCnt := lpCnt + 1
    end
    end;
    myChk
    end;

     

    Call it like this...

     

    aContains(concat(yourArray), chkValue)

    • Sean
    • 4 yrs ago
    • Reported - view

    This might not be applicable, but if you define the function in a formula field instead of in "Global script definitions" and declare the array (it can be empty) before the function definition then you don't have to convert the array to a string.

    • ioannis.1
    • 4 yrs ago
    • Reported - view

    Perfect 

    Thank you

Content aside

  • 2 Likes
  • 4 yrs agoLast active
  • 6Replies
  • 3899Views
  • 2 Following