The FooDoo Lounge

List handling

Just a couple of basic list manipulation handlers.

Add to a list

A fairly standard, though reasonably efficient and compact routine. The 1st line prevents the possibly of data sharing between the input and output lists, which can otherwise occur under some circumstances.

Pass the list to be added to, the index at which the new item is to be added & the item to add. Supports valid negative indices - the example below would return the same result if the index -2 was passed.

USAGE: set myList to insertListItem into {1, 2, 3, 4, 5} at 5 for "newItem"
        --> {1, 2, 3, 4, "newItem", 5}

Input:
        inputList list - the list to which the item is to be added.
        idx integer - the index (position in list) at which the item will be added; if the index is greater than the list length, the new item will be added to the end. If the index is less than 0, the item will be added at that position from the end.
        newItem anything - the new item to add.
Output: list - returns a new list with the item added at the supplied position. Will not change inputList.
Error: idx can't be 0, or an invalid negative index (-6 with a 5 item list for eg)

-- insertListItem -- by Richard Morton, 2002 --
-- Add an item to a list at the given index
-- Pass a list, the index & the item to add
to insertListItem into inputList at idx for newItem
    copy inputList to thisList -- avoid inadvertent data sharing
    tell thisList to if idx = 1 then
        set beginning to newItem
    else if idx > length or idx = -1 then
        set end to newItem
    else
        if idx < 0 then set idx to (idx + 1)
        return items 1 thru (idx - 1) & (newItem as list) & items idx thru -1
    end if
    return thisList
end insertListItem

Delete from a list

Also reasonably standard, though I originally wrote it to delete multiple items so added parameter checking to provide some interface parity with insertListItem. Recursive when passed a list of indices.

Pass the list, the index at which the item is to be deleted or a list of indices of items to delete. If deleting multiple items from the list, the indices must be in ascending order (as in the second example).

USAGE: set myList to deleteListItems out of {1, 2, 3, 4, 5} at 4
        --> {1, 2, 3, 5}

OR: set myList to deleteListItems out of {1, 2, 3, 4, 5} at {1, 3, 5}
        --> {2, 4}

Input:
        inputList list - the list from which the item(s) will be deleted.
        itemIndices integer or list of integers - the index (position in list) of the item(s) to be deleted.
Output: list - a new list with the item(s) deleted from the supplied position(s)
Error:

-- deleteListItems -- by Richard Morton, 2002 --
-- Delete one or more items from a list
-- Pass the list & an index, or list of indices
-- Index list must be in ascending order
to deleteListItems out of inputList at itemIndices
    set {errStr, errNum} to {"The “at” parameter in 'deleteListItems' requires an integer or list of integers.", -1703} -- errAEWrongDataType
    set idxClass to class of itemIndices -- parameter checking
    if idxClass = integer then
        set itemIndices to itemIndices as list
    else if idxClass = list then
        if class of item 1 of itemIndices is not integer then error errStr number errNum
    else
        error errStr number errNum
    end if
    set countIndices to length of itemIndices -- delete single items
    if countIndices = 1 then tell inputList to ¬
        if item 1 of itemIndices = 1 then
            return the rest
        else if inputList's length = item 1 of itemIndices then
            return items 1 through (itemIndices - 1)
        else
            return items 1 through (itemIndices - 1) & items (itemIndices + 1) through -1
        end if
    repeat with n from countIndices to 1 by -1 -- delete multiple items recursively from the end
        set inputList to deleteListItems out of inputList at (item n of itemIndices)
    end repeat
    return inputList
end deleteListItems

 

The FooDoo Lounge is Copyright © Richard Morton 2002-2005

|| url: http://www.foodoo.sunreal.com.au/code/lists.html
|| created: 4-Aug-03, 10:11 PM; updated: 4-Aug-03, 11:55 AM
|| size: 34079 bytes