Re: Why doesn't foreach return a value
- From: "tom.rmadilo" <tom.rmadilo@xxxxxxxxx>
- Date: Thu, 31 Jan 2008 08:01:33 -0800 (PST)
On Jan 31, 2:12 am, Fredderic <my-name-h...@xxxxxxxxxx> wrote:
For [while] and [for], the deal would be exactly the same, except that
one extra step is required to return the last iteration result. You'd
need to grab the result if the body completes naturally, and set it as
the loops result value after failing the condition (which would have
just set a new result value). Though there is some case for keeping
the condition result value instead (as opposed to [if] and (proposed)
[foreach] which return the result of body evaluation). So for those
two, it'd probably be best to forget about fiddling with saving the
body result, and just leave the interpreters result value as-is, just
as with [foreach]. But that's a separate topic.
I think you're right, it would be easy to test, at least with
[foreach]
Starting at line 1811 of generic/tclCmdAH.c:
result = TclEvalObjEx(interp, bodyPtr, 0, iPtr->cmdFramePtr, objc-1);
if (result != TCL_OK) {
if (result == TCL_CONTINUE) {
result = TCL_OK;
} else if (result == TCL_BREAK) {
result = TCL_OK;
break;
} else if (result == TCL_ERROR) {
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
"\n (\"foreach\" body line %d)",
interp->errorLine));
break;
} else {
break;
}
}
This executes the body code of foreach (in the parent frame) and
either breaks or continues, after resetting the result code to TCL_OK.
Once out of this loop (not shown), the next code nullifies the Tcl
result:
if (result == TCL_OK) {
Tcl_ResetResult(interp);
}
But I think I made a mistake about one thing in a previous post. The
loop code/frame/level doesn't cross the boundary of a proc. If you use
a bare [break] or [continue], you get an error. But, if you use
[return -code break/continue],
you can get out of the current level and then out of the loop.
.
- References:
- Re: Why doesn't foreach return a value
- From: Rufus V. Smith
- Re: Why doesn't foreach return a value
- From: Mark Janssen
- Re: Why doesn't foreach return a value
- From: tom.rmadilo
- Re: Why doesn't foreach return a value
- From: Darren New
- Re: Why doesn't foreach return a value
- From: tom.rmadilo
- Re: Why doesn't foreach return a value
- From: Darren New
- Re: Why doesn't foreach return a value
- From: tom.rmadilo
- Re: Why doesn't foreach return a value
- From: suchenwi
- Re: Why doesn't foreach return a value
- From: Fredderic
- Re: Why doesn't foreach return a value
- Prev by Date: Re: Why doesn't foreach return a value
- Next by Date: Re: How can I use tcl to read files written in GBK or GB18030 encoding?
- Previous by thread: Re: Why doesn't foreach return a value
- Next by thread: Re: Why doesn't foreach return a value
- Index(es):
Relevant Pages
|