user1270123
user1270123

Reputation: 573

How to pass dataset out of a proc in TCL

I'm reading a set of data line by line from a file in a procedure. Now how can I return these lines of data from this procedure. And how can I assign these data outputted from this procedure to a dataset?

proc readdata{} {
    set fptr [open Test1.txt r]
    set fptr_data [read $fptr]
    set data [split $fptr_data "\n"]  
}

How can I return this data from this procedure?

Upvotes: 1

Views: 177

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386285

You can use the return statement:

proc readdata {filename} {
    set fptr [open $filename r]
    set fptr_data [read $fptr]
    set data [split $fptr_data "\n"]
    return $data
}
...
set dataset [readdata Test1.txt]

However, by default Tcl procedures return the result of the last command that was run. Personally I prefer an explicit return statement.

Upvotes: 3

glenn jackman
glenn jackman

Reputation: 247062

Technically, you need do nothing else:

When a procedure is invoked, the procedure's return value is the value specified in a return command. If the procedure does not execute an explicit return, then its return value is the value of the last command executed in the procedure's body.

-- http://www.tcl.tk/man/tcl8.5/TclCmd/proc.htm

Upvotes: 3

Related Questions