Re: Why doesn't foreach return a value




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.
.



Relevant Pages

  • Re: How to modify the width of all pmos and nmos cell to their double in the opened schematic ?
    ... Get the list of instances/devices in your schematic: ... Loop all the properties and grab only those you are interested on ... );end foreach ...
    (comp.cad.cadence)
  • How to modify the width of all pmos and nmos cell to their double in the opened schematic ?
    ... Get the list of instances/devices in your schematic: ... Loop all the properties and grab only those you are interested on ... );end foreach ...
    (comp.cad.cadence)
  • Re: Count Lines in (Huge) Text Files
    ... A few years ago, I was doing some high-throughput disk stuff and my recollection is that I found the same thing you did: larger buffers only helped up to about 8K or so, and past that any improvement was minimal. ... me that with appropriate settings for its buffer, it should perform better, since it ought to be optimized for line-based i/o. ... Assuming what's hurting you in the explicit forloop is the retrieval of the data and not the counter increment, the above should perform basically as well as a plain foreach() loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: Lost data on socket - Can we start over politely?
    ... $clsel as a single-element-holding object containing the current ... simply performing the following check on $sel instead of $clsel? ... removing elements from something from a list used for the loop is a Bad ... Doesn't that throw the foreach() loop out of sync? ...
    (comp.lang.perl.misc)