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

Username:   Password: 
 RegisterRegister   
 flexible arrays in type record?!? (and also subprogram parameters)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chopperdudes




PostPosted: Sat Dec 20, 2008 9:44 pm   Post subject: flexible arrays in type record?!? (and also subprogram parameters)

unless i'm doing something wrong, flexible arrays can't be used as a typeSpec in a type record construct, which is rly inconvenient... is there any way around this (like how multi-D arrays can be resized)? or is it just stuck as it is and i'll have to find other ways out?

also flexible arrays can't be passed as var parameters to procedures, any ways around this too?

thx in advance
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Sat Dec 20, 2008 10:27 pm   Post subject: RE:flexible arrays in type record?!? (and also subprogram parameters)

Turing:
proc foo (var bar : array 1 .. * of int)
    bar (1) := 42
    put upper (bar)
end foo

var baz : flexible array 1 .. 0 of int

for i : 1 .. Rand.Int (1, 100)
    new baz, upper (baz) + 1
    baz (upper (baz)) := 1
end for

foo (baz)
chopperdudes




PostPosted: Sat Dec 20, 2008 10:33 pm   Post subject: RE:flexible arrays in type record?!? (and also subprogram parameters)

clayton i think you misunderstood me. of course that will work, but it wont work if i want to new the var array bar inside the procedure.

instead if you want to new a flexible array inside a procedure you can't pass parameter and you can only use a global flexible array.

and does any1 know any possible way around the type record part? that's my biggest inconvenience right now.
Clayton




PostPosted: Sat Dec 20, 2008 10:38 pm   Post subject: RE:flexible arrays in type record?!? (and also subprogram parameters)

Let me ask you this then: Why are you passing a dynamic array to a procedure that will mutilate your precious variable? Why not have a function to return an array that you can use instead?
chopperdudes




PostPosted: Sat Dec 20, 2008 10:45 pm   Post subject: Re: flexible arrays in type record?!? (and also subprogram parameters)

well for example (and i'm making this on the top of my head):

Turing:


proc readFile (fileName: string, var list: flexible array 1..* of int)
var fileNum : int
open : fileNum, fileName, get
loop
exit when eof (fileName)
new list, upper (list) +1
get : fileNum, list (upper(list))
end loop
close : fileNum
end readFile


var phone, age : flexible array 1.. 0 of int

readFile ("phoneNumbers.txt", phone)
readFile ("age.txt", age)





Clayton




PostPosted: Sun Dec 21, 2008 11:29 am   Post subject: RE:flexible arrays in type record?!? (and also subprogram parameters)

Turing:
class FileHandler
    export readFile, saveFile, closeFile, getMaxElements, getContentAt
   
    var fileContents : flexible array 1 .. 0 of string
    var fileName : string
    var stream : int
   
    proc initialize (filename_ : string)
        fileName := filename_
    end initialize
   
    fcn getMaxElements : int
        return upper (fileContents)
    end getMaxElements
   
    fcn getContentsAt (i : int) : string
        return fileContents (i)
    end getContentAt
   
    proc readFile
        open : stream, fileName, get
        loop
            exit when eof (fileName)
            new fileContents, upper (fileContents) + 1
            get : stream, fileContents (upper (fileContents))
        end loop
    end readFile
   
    proc saveFile
        open : stream, fileName, put
        for i : 1 .. upper (fileContents)
            put : stream, fileContents (i)
        end for
    end saveFile
   
    proc closeFile
        close : stream
    end closeFile
end FileHandler


Implemented with:

Turing:
var file : pointer to FileHandler

new FileHandler, file

file -> initialize ("foo.txt")
file -> loadFile
file -> closeFile

for i : 1 .. file -> getMaxElements
    put file -> getContentAt (i)
end for

Note: This is untested, but shows the general idea.
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  [ 6 Posts ]
Jump to:   


Style:  
Search: