2 Replies Latest reply on Sep 3, 2007 6:55 PM by James Newton, ACP

    formatted file input of integers

    beemerbiker
      I can see how to do low-level file i/o and how to read in strings in Lingo, but I have a long list/array of numbers( integers) that I want to read into the program from a file once and for all at the start of the movie. Then later, I want to use these integers to control the movie display. Is there any easy way to do formatted file reads in Lingo, or do I have to write my own number-parsing routine? Thanks for any help.
      John
        • 1. Re: formatted file input of integers
          McFazstp Level 1
          If you simply store a list in a text file you can get the list back by getting the value

          ie. put value( "[1, 2, 3]" )
          -- [1, 2, 3]

          If you are storing integers it shoudn't be an issue but if you are storing strings there are some characters that will stop the list being interpreted correctly

          ie.
          put value( "["a string with a quote " in it"]" )
          -- <Void>
          put value( "["a string with a quote "&quote&" in it"]" )
          -- ["a string with a quote " in it"]
          • 2. Re: formatted file input of integers
            James Newton, ACP Level 3
            Here are a couple of handlers that should return a string whose value() is the original list:

            on GetStringList(aList) ----------------------------------------------
            -- INPUT: <aList> should be a linear or property list
            -- OUTPUT: Returns a string whose value is the original list. The
            -- use of embedded quotes in strings and the appearance of
            -- <Void> are explicitly treated. Void in sublists will
            -- be correctly treated, but strings with embedded quotes
            -- will only be treated in the outermost values
            --------------------------------------------------------------------

            if listP(aList) then
            aList = aList.duplicate() -- so that we don't change the original
            i = aList.count

            repeat while i
            tValue = aList

            case ilk(tValue) of
            #string:
            if tValue contains QUOTE then
            -- Replace embedded quotes
            aList
            = ReplaceAll(tValue,QUOTE,QUOTE&"&QUOTE&"&QUOTE)
            end if
            end case
            i = i - 1
            end repeat
            end if

            tString = string(aList)

            -- Replace any occurrences of <Void> with Void
            tString = ReplaceAll(tString, "<Void>", "Void")

            return tString
            end GetStringList


            on ReplaceAll(aString, aSubString, aReplacement) ---------------------
            -- INPUT: <aString> is the main string to search in
            -- <aSubString> is a string which may appear in <aString>
            -- <aReplacement> is a string which is to replace all
            -- occurrences of <aSubString> in <aString>
            -- OUTPUT: returns an updated version of <aString> where all
            -- occurrences of <aSubString> have been replaced by
            -- <aReplacement>
            --------------------------------------------------------------------

            if aSubString = "" then
            return aString
            end if

            tTreatedString = ""
            tLengthAdjust = the number of chars of aSubString - 1

            repeat while TRUE
            tOffset = offset(aSubString, aString)
            if tOffset then
            if tOffset - 1 then
            put chars(aString, 1, (tOffset - 1)) after tTreatedString
            end if

            put aReplacement after tTreatedString
            delete char 1 to (tOffset + tLengthAdjust) of aString

            else -- there are no more occurrences
            put aString after tTreatedString
            return tTreatedString
            end if

            end repeat
            end ReplaceAll