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) dolet myArray := split(replace(ary, ", ", ","), ",");let idxValue := false;if aryIndex < count(myArray) thenidxValue := trueend;idxValueend;
function ahfIsEmpty(ary : text) dolet myArray := split(replace(ary, ", ", ","), ",");if count(myArray) > 0 then false else true endend;
function aAppend(ary : text,newItem : text) dolet myArray := split(replace(ary, ", ", ","), ",");if length(newItem) != 0 thenif not ahfIsEmpty(ary) thenmyArray := split(replace(ary, ", ", ",") + "," + newItem, ",")elsemyArray := split(newItem, ",")endelsealert("aAppend( ) : New array item can't be empty!")end;myArrayend;
function idxInsert(ary : text,insIndex : number,insItem : text) dolet myArray := split(replace(ary, ", ", ","), ",");let iI := abs(insIndex);if ahfChkIndex(ary, iI) thenlet insLoc := item(myArray, iI);if iI != count(myArray) - 1 thenmyArray := split(replace(replace(ary, insLoc + ",", insItem + "," + insLoc + ","), ", ", ","), ",")elsemyArray := split(replace(replace(ary, insLoc, insItem + "," + insLoc), ", ", ","), ",")endelsemyArray := aAppend(ary, insItem)end;myArrayend;
function idxRemove(ary : text,rmvIndex : number) dolet myArray := split(replace(ary, ", ", ","), ",");if not ahfIsEmpty(ary) thenlet rI := abs(rmvIndex);if ahfChkIndex(ary, rI) thenlet rmvItem := item(myArray, rI);if index(ary, rmvItem) + length(rmvItem) != length(ary) thenmyArray := split(replace(replace(ary, rmvItem + ", ", ""), ", ", ","), ",")elsemyArray := split(replace(replace(ary, rmvItem, ""), ", ", ","), ",")endelsealert("idxRemove( ) : Index [" + rI + "] is out of range [0.." + text(count(myArray) - 1) + "]")endelsealert("idxRemove( ) : The array is empty!")end;myArrayend;
function idxMove(ary : text,movFromIndex : number,movToIndex : number) dolet myArray := split(replace(ary, ", ", ","), ",");if not ahfIsEmpty(ary) thenlet mFI := abs(movFromIndex);let movItem := item(myArray, mFI);if ahfChkIndex(ary, mFI) thenlet mTI := abs(movToIndex);if ahfChkIndex(ary, mTI) thenmyArray := idxRemove(ary, mFI);myArray := idxInsert(concat(myArray), mTI, movItem)elsemyArray := idxRemove(ary, mFI);myArray := aAppend(concat(myArray), movItem)endelsealert("idxMove( ) : Index [" + mFI + "] is out of range [0.." + text(count(myArray) - 1) + "]")endelsealert("idxMove( ) : The array is empty!")end;myArrayend;
function idxReplace(ary : text,rplIndex : number,rplItem : text) dolet myArray := split(replace(ary, ", ", ","), ",");if not ahfIsEmpty(ary) thenlet rI := abs(rplIndex);if ahfChkIndex(ary, rI) thenlet oldItem := item(myArray, rI);if index(ary, oldItem) + length(oldItem) != length(ary) thenmyArray := split(replace(replace(ary, oldItem + ", ", rplItem + ", "), ", ", ","), ",")elsemyArray := split(replace(replace(ary, oldItem, rplItem), ", ", ","), ",")endelsealert("idxReplace( ) : Index [" + rI + "] is out of range [0.." + text(count(myArray) - 1) + "]")endelsealert("idxReplace( ) : The array is empty!")end;myArrayend;
function idxSwap(ary : text,idxOne : number,idxTwo : number) dolet myArray := split(replace(ary, ", ", ","), ",");if not ahfIsEmpty(ary) thenif count(myArray) >= 2 thenlet iO := abs(idxOne);let itmOne := item(myArray, iO);if ahfChkIndex(ary, iO) thenlet iT := abs(idxTwo);let itmTwo := item(myArray, iT);if ahfChkIndex(ary, iT) thenlet itmTmp := "#%$";if index(ary, itmOne) + length(itmOne) != length(ary) thenary := replace(ary, itmOne + ", ", itmTmp + ", ")elseary := replace(ary, itmOne, itmTmp)end;if index(ary, itmTwo) + length(itmTwo) != length(ary) thenary := replace(ary, itmTwo + ", ", itmOne + ", ")elseary := replace(ary, itmTwo, itmOne)end;ary := replace(ary, itmTmp, itmTwo);myArray := split(replace(ary, ", ", ","), ",")elsealert("idxSwap( ) : Index [" + iT + "] is out of range [0.." + text(count(myArray) - 1) + "]")endelsealert("idxSwap( ) : Index [" + iO + "] is out of range [0.." + text(count(myArray) - 1) + "]")endelsealert("idxSwap( ) : There must be 2 or more items in the array!")endelsealert("idxSwap( ) : The array is empty!")end;myArrayend
If you find any errors please post them.
7 replies
-
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) dolet myArray := split(replace(ary, ", ", ","), ",");if item(myArray, aryIndex) = chkValue thentrueelsefalseendend -
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) dolet aryCount := count(split(replace(ary, ", ", ","), ","));let myIndex := 0;while myIndex < aryCount and not idxContains(ary, myIndex, chkValue) domyIndex := myIndex + 1end;myIndexend;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) doa1 := aAppend(concat(a1), "Test " + i)end;let myIndex := aryContains(concat(a1), "Test 5");if myIndex < count(a1) thena1 := idxReplace(concat(a1), myIndex, "Hello")end;a1 -
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
-
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()orjoin(). 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) dolet ar := split(txt, ",");let myChk := false;let lpCnt := 0;while lpCnt < cnt(ar) and not myChk doif item(ar, lpCnt) = chk thenmyChk := trueelselpCnt := lpCnt + 1endend;myChkend;Call it like this...
aContains(join(yourArray, ","), chkValue)This works for
concat()...function aContains(txt : text,chk : text) dolet ar := split(replace(txt, ", ", ","), ",");let myChk := false;let lpCnt := 0;while lpCnt < cnt(ar) and not myChk doif item(ar, lpCnt) = chk thenmyChk := trueelselpCnt := lpCnt + 1endend;myChkend;Call it like this...
aContains(concat(yourArray), chkValue) -
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.
-
Perfect
Thank you
-
Thanks for providing solutions
Content aside
-
2
Likes
- 1 yr agoLast active
- 7Replies
- 3965Views
-
4
Following
