Function: ListAddAt
Function: ListAddAt

Function: ListAddAt

ListAddAt ( LIST, ITEM, INDEX )

Adds an item to a list at a specific index.

Parameters

Parameter Type Description
LIST VALUE(LIST) The list to revome from.
ITEM VALUE The item to search for and remove.
INDEX VALUE(INTEGER) The 0-based index to add the value at.

Returns

VALUE(BOOLEAN) TRUE if added, FALSE if not.

What It Does

This function adds a value at a specific index in a list, and returns TRUE if added. If the provided list is not a list, or the index provided is less than 0, nothing is added and this returns FALSE. If the index is greater than or equal to the list's length, it is added to the end of the list. The item can be any type, and is added as-is without any conversion. The index is always converted to an integer, if it is not an integer.

A successful add increases the list's size by 1.

Example

List Functions Example


world
{
	start()
	{
		local ret;
		local list;

		// New
		list = listNew(5);
		textln(list);
		local x = 1;
		while (x <= length(list))
		{
			list[x - 1] = x;
			x = x + 1;
		}
		textln(list);

		// Add		
		ret = listAdd(list, 6);						// list = [1, 2, 3, 4, 5, 6], returns true
		textln(list + ": " + ret);
		ret = listAdd(list, "apple");				// list = [1, 2, 3, 4, 5, 6, "apple"], returns true
		textln(list + ": " + ret);

		// AddAt
		ret = listAddAt(list, 7, 2);				// list = [1, 2, 7, 3, 4, 5, 6, "apple", "banana"], returns true
		textln(list + ": " + ret);
		ret = listAddAt(list, "banana", 10);		// list = [1, 2, 7, 3, 4, 5, 6, "apple", "banana"], returns true
		textln(list + ": " + ret);

		// Remove
		ret = listRemove(list, 3);					// list = [1, 2, 7, 4, 5, 6, "apple", "banana"], returns true
		textln(list + ": " + ret);
		ret = listRemove(list, 100);				// list = [1, 2, 7, 4, 5, 6, "apple", "banana"], returns false 
		textln(list + ": " + ret);

		// RemoveAt
		ret = listRemoveAt(list, 0);				// list = [2, 7, 4, 5, 6, "apple", "banana"], returns 1
		textln(list + ": " + ret);
		ret = listRemoveAt(list, 50);				// list = [2, 7, 4, 5, 6, "apple", "banana"], returns false
		textln(list + ": " + ret);

		// Index
		textln(listIndex(list, 4));					// 2
		textln(listIndex(list, 1));					// -1
		textln(listIndex(list, "4"));				// -1

		// Contains
		textln(listContains(list, 4));				// true
		textln(listContains(list, 1));				// false
		textln(listContains(list, "4"));			// false

		// Concatenation
		textln(listConcat([1, 2, 3], [4, 5, 6]));	// [1, 2, 3, 4, 5, 6]
		textln(listConcat([1, 2, 3], 4));			// [1, 2, 3, 4]
		textln(listConcat("apple", "orange"));		// ["apple", "orange"]

		quit;
	}
}
×

Modal Header