Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Having problems with a unit file and database
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
WOIKeller




PostPosted: Fri Oct 16, 2009 10:01 am   Post subject: Having problems with a unit file and database

The only problem I'm having is that turring can't export a variable from a module. What should I do?

code:

    export databaseInfo, getDatabaseInfo, dataArray %Exports the callable parts of the program.

    var streamIO : int %Used for keeping track of the file stream.
    var row : string %Used to retreive an entire record of data from the database.
    var file : string %Holds the file name of the database.
    var delimiter : char %Holds the delimetre used in the database.

    var dataLengthIndex : flexible array 1 .. 0, 1 .. 0 of int %Holds the lengths of all fields.
    var dataArray : flexible array 1 .. 0, 1 .. 0 of string %Stores all the data for calling by the programer.

    proc databaseInfo (fileInput : string, delimiterInput : char)
        %Gets all of the data from the programer and stores it for future uses.
        file := fileInput
        delimiter := delimiterInput
    end databaseInfo

    function countRecords : int %Counts the number of records.
        var numRecords : int := 0

        open : streamIO, file, get
        loop
            get : streamIO, row : *
            numRecords += 1
            exit when eof (streamIO)
        end loop
        close : streamIO

        result numRecords
    end countRecords

    function countFields : int %Counts the number of fields in the first line.
        var numDelimeters : int := 0

        open : streamIO, file, get
        get : streamIO, row : *
        for i : 1 .. length (row)
            if row (i) = delimiter then
                numDelimeters += 1
            end if
        end for

        numDelimeters += 1

        result numDelimeters
    end countFields

    proc sizeArrays %Sets the lengths of the 2 flexible arrays.
        new dataLengthIndex, countRecords, countFields
        new dataArray, countRecords, countFields
    end sizeArrays

    proc getLengthOfEachRecord %Gets the size of each field and stores it in the index array.
        var fieldNum : int := 0
        for recordNum : 1 .. countRecords
            dataLengthIndex (recordNum, fieldNum) := 0
            for j : 1 .. length (row)
                if row (j) = delimiter then
                    fieldNum += 1
                elsif row (j) = "\"" then %Is used to remove the quotes from strings.
                else
                    dataLengthIndex (recordNum, fieldNum) += 1
                end if
            end for
        end for
    end getLengthOfEachRecord

    proc populateArray %Stores each record in an array for exporting to the user.
        open : streamIO, file, get

        for recordNum : 1 .. countRecords
            for fieldNum : 1 .. countRecords
                get streamIO, dataArray (recordNum, fieldNum) : dataLengthIndex (recordNum, fieldNum)
            end for
        end for
    end populateArray

    proc getDatabaseInfo
        getLengthOfEachRecord
        populateArray
    end getDatabaseInfo

end DatabaseIO
[/code]
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Oct 16, 2009 11:49 am   Post subject: RE:Having problems with a unit file and database

You can export a function that gives you access to that variable's value.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Superskull85




PostPosted: Fri Oct 16, 2009 11:32 pm   Post subject: Re: Having problems with a unit file and database

You can export a variable from within a module in Turing. However, if you want to change the variable you will have to add "var" in front of the export item to make the variable writable.

I don't know why you are getting an error, it may be because of your version of Turing. I used version 4.0.5 for my tests.

It is much better to create a procedure/function that changes the variables value though.
WOIKeller




PostPosted: Mon Oct 19, 2009 9:47 am   Post subject: RE:Having problems with a unit file and database

Thank you ever so much. I have fixed the array access problem, but as with any program 15 more problems have shown up XD. Thanks for all the help.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: