Re: Why doesn't foreach return a value



You are attempting to return a non-initialized variable as
the body of the foreach command never is executed.

I'm not a tcl expert, but what would this return, may I ask:

proc test {l} {
set itemcount 0
foreach a $l {incr itemcount}
return $itemcount
}

An empty list should return zero itemcount, it would return
one in your scheme, wouldn't it?

As would a single-element list.

(never mind that you should use llength $l )

Rufus


"Mark Janssen" <mpc.janssen@xxxxxxxxx> wrote in message
news:8cb11668-0180-4297-a8b8-8a99889a3815@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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

  • Why doesnt foreach return a value
    ... Is there any specific reason a call to foreach always returns? ... proc test { ... foreach a $l {append b $a} ...
    (comp.lang.tcl)
  • Re: [Feature Request] "first:" "last:" sections in a "foreach" block
    ... > foreach means foreach means foreach, so for each item of a collection of ... It could be that I want to execute this ... Order returned by an enumerator is only statically defined during the ... Syntax and semantics are another thing. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [Feature Request] "first:" "last:" sections in a "foreach" block
    ... > I would think that by the definition of foreach, the language construct ... You don't need to know the ordinality though - you only need to know ... and the "last" bit you just execute ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [Feature Request] "first:" "last:" sections in a "foreach" block
    ... foreach means foreach means foreach, so for each item of a collection of ... I would not like to see a switch inside the loop that test ... It could be that I want to execute this ... execute code for. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: threads
    ... This creates a single thread to execute the block. ... $threads->create and async has to do with whether you're compfortable ... foreach { ...
    (perl.beginners)