Re: New question on strings



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)} {
....
}
}

.



Relevant Pages

  • Re: Possible to create an array and call object constructors at the same time?
    ... I initialize my array and then loop through the array, ... But at some point, you have to have the loop, and you have to write it yourself. ... Initializing an array of reference types, with each member referencing an instance that is identical to every other member seems potentially inefficient. ... It may be better to defer initialization of any given element until you actually need that specific element of the array. ...
    (comp.lang.java.programmer)
  • Re: New question on strings
    ... BTW, initialization is a complex subject. ... Considering the fact that [incr] essentially implies a ... loop of some type, that is some code is being executed multiple times, ...
    (comp.lang.tcl)
  • Re: LOCALISATION
    ... "is required after the loop is executed." ... You have just such an initialization in your code. ... To make the value of the CV visible to the exception handler. ... Does PL/I have dynamic arrays of some kind so you can start out with an empty array and as it grows you can simply check the size of the thing? ...
    (comp.lang.pl1)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Kevin Backmann ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)