Hello out there,Never mind, I think I found a simple solution: faking 'gets':
I have a script which expects to receive its input via a file handle.
I, however, have the prospective input in form of a string in memory
and I would hate to have to write it to a file just to be able to use
the script.
Q: Is there some mechanism to get a file a handle to memory which I
then could pass to 'gets'? I thought of 'memchan' but wasn't
successful.
Any help or tip will be greatly appreciated
Helmut
Helmut Giese schrieb:
I thought of 'memchan' but wasn't
successful.
Q: Is there some mechanism to get a file a handle to memory which I
then could pass to 'gets'? I thought of 'memchan' but wasn't
successful.
You meant tcl::chan::string, didn't you?ähem, no - I really meant memchan.
I remember me also quitting these memchans, but I cannot recall the exact reason/problem.I only did really naively something like
Can your issue be reproduced/presented with feasible effort?
What do you mean with "memory"? Is the data a constant in your script,that's a good tip. I wasn't aware of the 'channel' command.
do you have a variable that contains the data, is the data the result of
some command, or something else?
If the data is in a variable, you can use tcl::chan::variable:
package require tcl::chan::variable
set var "Some string\ncontaining multiple lines"
set fd [::tcl::chan::variable var]
puts [gets $fd]
puts [tell $fd]
seek $fd 0
puts [read $fd]
close $fd
For other cases, tcl::chan::string may be a better fit. It works pretty
much the same as the above code.
* Helmut Giese <hgiese@ratiosoft.com>
| >I remember me also quitting these memchans, but I cannot recall
| >the exact reason/problem.
| >Can your issue be reproduced/presented with feasible effort?
| I only did really naively something like
| set chan [tcl::chan::memchan]
| set txt <some file>
| puts $chan $txt
| while {[gets $chan line] >= 0} {
| puts "[incr lineNumber]: $line"
| }
| which didn't work.
Playing around with that it seems that the input and output positions
of the stream share a common state, i.e. after the 'puts $chan $txt'
the reading side is also at EOF.
If you do a seek to the beginning
puts $chan $txt
chan seek $chan 0
prior to the following gets, you can read the $txt line by line.
Hi heinrichmartin,
You meant tcl::chan::string, didn't you?ähem, no - I really meant memchan.
I remember me also quitting these memchans, but I cannot recall theI only did really naively something like
exact reason/problem.
Can your issue be reproduced/presented with feasible effort?
set chan [tcl::chan::memchan]
set txt <some file>
puts $chan $txt
while {[gets $chan line] >= 0} {
puts "[incr lineNumber]: $line"
}
which didn't work.
You forgot to 'seek $chan 0' there to rewind the channel to theTcl never ceases to surprise me. Thank you both, Rich and Ralf, for
beginning before trying to read it. You should have gotten an eof >immediately with that code, which would be why it did not seem to work.
$ rlwrap tclsh
% package require tcl::chan::memchan
1.0.4
% set chan [tcl::chan::memchan]
rc0
% set txt "line 1\nline 2\nline 3\n"
line 1
line 2
line 3
% puts $chan $txt
% seek $chan 0
% while {[gets $chan line] >= 0} {
puts "[incr lineNumber]: $line"
}
1: line 1
2: line 2
3: line 3
4:
%
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 297 |
Nodes: | 16 (2 / 14) |
Uptime: | 34:24:53 |
Calls: | 6,669 |
Calls today: | 1 |
Files: | 12,216 |
Messages: | 5,338,371 |