Generalized Array Handlers!
Rob Cozens
rcozens at pon.net
Tue Jul 13 11:51:35 EDT 2004
Hi All,
Here is a lightly-tested set of generalized array handlers:
The coordinates are now passed as a comma-delimited list, and the
delimiters are passed & parsed character-by-character (so return,
space, & comma can be used as delimiters). Note: NO bounds checking
is done; so addressing an element outside the array WILL create new
elements. Also, coordinates are assumed to be integers, and no
integrity checking is done.
function getElement @theArray,theCoordinates,theDelimiters
put item 1 of theCoordinates into coordinateNumber
set the itemDelimiter to (char 1 of theDelimiters)
get item coordinateNumber of theArray
set the itemDelimiter to comma
delete item 1 of theCoordinates
delete char 1 of theDelimiters
if theCoordinates is empty then return it
if theDelimiters is empty then return "Coordinate--Delimiter Mismatch"
return getElement(it,theCoordinates,theDelimiters)
end getElement
(I tried to script putElement recursively; but couldn't come up with
a design that didn't require duplicating the entire array with each
iteration; so opted for a repeat that only duplicates the array
elements affected.)
on putElement theElement, at theArray,theCoordinates,theDelimiters
put the number of items of theCoordinates into coordinateCount
put 0 into currentCoordinate
repeat
add 1 to currentCoordinate
if currentCoordinate = coordinateCount then exit repeat
put item currentCoordinate of theCoordinates into coordinateNumber
get char currentCoordinate of theDelimiters
if currentCoordinate = 1 then put
getElement(theArray,coordinateNumber,it) into subArray[1]
else
put subArray[currentCoordinate-1] into tempArray
put getElement(tempArray,coordinateNumber,it) into
subArray[currentCoordinate]
end if
end repeat
put item coordinateCount of theCoordinates into coordinateNumber
set the itemDelimiter to char coordinateCount of theDelimiters
if coordinateCount = 1 then -- 1 level "arrays"
put theElement into item coordinateNumber of theArray
exit putElement
else put theElement into item coordinateNumber of subArray[coordinateCount-1]
subtract 1 from currentCoordinate
repeat
set the itemDelimiter to comma
put item currentCoordinate of theCoordinates into coordinateNumber
get char currentCoordinate of theDelimiters
set the itemDelimiter to it
subtract 1 from currentCoordinate
if currentCoordinate = 0 then
put subArray[1] into item coordinateNumber of theArray
exit repeat
else
put subArray[currentCoordinate+1] into item coordinateNumber of
subArray[currentCoordinate]
end if
end repeat
end putElement
Enjoy! (I have a simple stack I used for testing...if anyone wants a
copy, write me privately.)
Rob Cozens
CCW, Serendipity Software Company
"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."
from "The Triple Foole" by John Donne (1572-1631)
More information about the use-livecode
mailing list