At 09:14 AM 2/15/02 -0500, Robert Rycroft wrote:
>Is there any way to use Lingo to work with a n x m matrix?  For example, 
>let's say I have data that could be stored in a matrix with 100 rows and 2 
>columns.  I seem to recall reading about it somewhere.  Something about 
>putting linear lists inside linear lists, but cannot find the source and 
>cannot figure out how to access the specific elements of the matrix.  For 
>example, how would I access the element in row 62, column 2?
>
>I am trying to use Lingo to write a simulation that was originally written 
>in BASIC.  In BASIC multi-dimensional matrices are easy to work with.
>
>Thanks.
>
>Robert Rycroft
>[EMAIL PROTECTED]



Here is a generic array example.  As a good exercise, convert it to an object!

Note that you don't call the 'array' handler directly.  Instead, use the 
defineArray, setArray, and getArray functions.  I just made an 'array' 
function so I didn't have to duplicate the boundary checks.  The other 
functions follow the 'array' function.

-- Array example
-- Tab Julius ([EMAIL PROTECTED])
-- 2/15/2002


on array func, whichArray, x, y, newValue

   if (not listP(whichArray)) then
     -- Uh oh, not a list!
     return(#Error)
   end if

   if (not y or y > count(whichArray)) then
     -- Y out of bounds!  Too low or too high
     return(#Error)
   end if

   subArray =getAt(whichArray, y)
   if (not x or x > count(subArray)) then
     -- X out of bounds!  Too low or two high
     return(#Error)
   end if

   thisElement =getAt(subArray, x)      -- Get the current one regardless
   if (func = #Set) then
     setAt(subArray, x, newValue)
   end if
   return(thisElement)
end




on defineArray maxX, maxY
   -- Build an array x by y.  Call this first, before getArray or getArray

   newArray =[]                         -- This is the master list
   repeat with y =1 to maxY             -- Go through each "row"
     newSubArray =[]                    -- Define a sublist
     setAt(newSubArray, maxX, 0)        -- And create the maximum entry
     add(newArray, newSubArray)         -- And add it to the master list
   end repeat

   return(newArray)                     -- Return back to caller
end




on setArray whichArray, x, y, newValue
   -- Call this function to set a value in your array
   -- It returns the previous value that was there, or #Error
   result =array(#Set, whichArray, x, y, newValue)
   return(result)
end



on getArray whichArray, x, y
   -- Call this function to get a value from your array
   result =array(#Get, whichArray, x, y)
   return(result)
end


Use it like this:


   myArray =defineArray(5, 5)
   put myArray
   -- [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0]]

   setArray(myArray, 3, 2, "Test")
   setArray(myArray, 5, 5, 99)

   put myArray
   -- [[0, 0, 0, 0, 0], [0, 0, "Test", 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 
0], [0, 0, 0, 0, 99]]

   put getArray(myArray, 7, 7)
   -- #error

   put getArray(myArray, 3,1)
   -- 0

   put getArray(myArray, 3, 2)
   -- "Test"

   put getArray(myArray, 5, 5)
   -- 99

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email 
[EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for 
learning and helping with programming Lingo.  Thanks!]

Reply via email to