Why doesn't foreach return a value



Is there any specific reason a call to foreach always returns {}? It
would be useful if it returns the last result. I could have used this
on occasion. For instance a bit of a crafted example:

proc test {l} {
foreach a $l {append b $a}
return $b
}

fails in the case $l eq {}. It needs to be:

proc test {l} {
set b {}
foreach a $l {append b $a}
return $b
}

with foreach returning a value (or {} if the body doesn't execute)
this would be:

proc test {l} {
foreach a $l {append _ $l}
}

Mark
.



Relevant Pages

  • Re: Why doesnt foreach return a value
    ... the body of the foreach command never is executed. ... proc test { ... foreach a $l {incr itemcount} ... with foreach returning a value (or if the body doesn't execute) ...
    (comp.lang.tcl)
  • Re: String parameter indexer question.
    ... array in the 'for' case then object enumeration using 'foreach'. ... then I heard otherwise which is my main reason for asking. ... primarily I wanted to have a string parameter indexer ... :>: I have a custom class and a collection for that custom class that ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Holub on getters/setters again
    ... There is, as far as I can see, no reason to have the ArrayList at ... >>before the client that uses it was written. ... And I knew I wanted to call foreach on it from the very beginning, ... whether the element already exists in that container. ...
    (comp.object)
  • Re: Holub on getters/setters again
    ... >before the client that uses it was written. ... When the servers implementation is running the show, ... And I knew I wanted to call foreach on it from the very beginning, ... The reason that I didn't implement foreach was that it is slightly tricky, ...
    (comp.object)
  • Re: extract words from an array
    ... "teach yourself perl" book there is an activity ... foreach $singleline { ... if you're really looking for shorter, there's no reason for the ... Those words are thoughts and yardstick. ...
    (perl.beginners)