Re: Why doesn't foreach return a value
- From: "Rufus V. Smith" <nospam@xxxxxxxxxx>
- Date: Mon, 28 Jan 2008 15:10:04 -0500
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
.
- Follow-Ups:
- Re: Why doesn't foreach return a value
- From: Mark Janssen
- Re: Why doesn't foreach return a value
- Prev by Date: Re: Core commands as ensembles...
- Next by Date: Re: TkTable Mousewheel Binding for Vertical Scrolling
- Previous by thread: twapi and (password never expires)
- Next by thread: Re: Why doesn't foreach return a value
- Index(es):
Relevant Pages
|