Re: New question on strings
- From: "tom.rmadilo" <tom.rmadilo@xxxxxxxxx>
- Date: Thu, 28 Feb 2008 13:23:14 -0800 (PST)
On Feb 28, 10:28 am, Bryan Oakley <oak...@xxxxxxxxxxxxxxxxxxxx> wrote:
Many feel the first is preferable to the second -- one line to do an
incr versus five lines to do a test and an incr or a set.
<shrug>
I think now that the feature is part of the language it's here to stay,
so there's not much point in debating it.
Yes, I agree you can make the argument in the given case, and I agree
that this is now part of the language, and I agree that it follows
along with the changes to [lappend] etc., so given that:
This only works for initialization the first time through. If you loop
through this section of code a second time, you have to, I guess,
delete the array (if it wasn't an array it would become impossible to
cleanup). But the code example suffers from a lack of documentation
you would get by initializing the array at the start of each use of
the loop. These problems seldom show up in simple code, but impact
reuse of this type of loop code. You can't test for this, because
working code works. But now broken code also works, or at least
appears to work. The same thing happens with [lappend] when applied to
global/namespace variables. In both cases, the solution doesn't
address the initialization problem, which you should do 'once',
'explicitly', and using the same code. So, in the example above, I
would probably do something like this:
namespace eval ::profile {
variable countInit {crit1 0 crit2 0 crit3 0 ... critn 0}
}
proc ::profile::lines {lines arVar} {
variable countInit
upvar 1 $arVar count
array set count $countInit
foreach line $lines {
if {<some criteria is met>} {
incr count(<some index>)
}
}
The question is why? Well, my guess is that somewhere after the loop
code is finished filling the array, you may need to use the
information:
array set initialCount $::profile::countInit
::profile::lines $lines count ;# hey, only one line, plus reuse, plus
compiled.
foreach criteria [array names initialCount] {
if {$count($criteria) != $initialCount($criteria)} {
....
}
}
.
- Follow-Ups:
- Re: New question on strings
- From: Andreas Leitgeb
- Re: New question on strings
- References:
- New question on strings
- From: mark
- Re: New question on strings
- From: mark
- Re: New question on strings
- From: Gerald W. Lester
- Re: New question on strings
- From: tom.rmadilo
- Re: New question on strings
- From: Neil Madden
- Re: New question on strings
- From: tom.rmadilo
- Re: New question on strings
- From: Bryan Oakley
- New question on strings
- Prev by Date: Re: need a tiny help with my SWIG'd program
- Next by Date: Re: tk_getOpenFile bug in Tk8.5.1
- Previous by thread: Re: New question on strings
- Next by thread: Re: New question on strings
- Index(es):
Relevant Pages
|