repeat string: how ?

Jan Decroos jan.decroos at groepvanroey.be
Tue Nov 19 04:52:01 EST 2002


use-revolution at lists.runrev.com writes:
>Jan,
>
>How about:
>
>function InitContainer pKind,pCount,p1,p2,p3
>    if pCount<=0 then return("")
>    if pCount= "" then return "Invalid number of arguments!"
>    put 1 into tNumSteps
>    put 1 into pStartNum
>    switch pKind
>        case "n"
>            put p1 into pStartNum
>            put p2 into tNumSteps
>            put p3 into tSeparator
>            break
>        case "s"
>            put p1 into tString
>            put p2 into tSeparator
>            break
>    end switch
>    put "" into tResult
>    set the lineDel to tSeparator
>    repeat with x = pStartNum to (pStartNum+pCount) step tNumSteps
>        if pKind = "n" then put x into line x of tResult
>        if pKind = "s" then put tString into line x of tResult
>    end repeat
>    return tResult
>end InitContainer
>
>This is off the top of my head and hasn't been tested

Ken,

Some remarks, if I may ...

*) Your script asumes all arguments p1, p2 and p3 are filled when InitContainer
is called.
My function uses default values when the 3rd, the 4th or the 5th argument are
not used.
See examples in my prev. mail :
	initcontainer("n",50)			--> returns a list of 50 lines from 1 to 50
	initcontainer("n",50,101)			--> returns a list of 50 lines from 101 to 150
	initcontainer("n",50,101,2)		--> returns a list of 50 lines from 101 to 199
(step 2)
	initcontainer("n",50,101,2,comma)	--> returns a list of 50 items from 101 to
199

	initcontainer("s",50,"Revolution")	--> returns a list of 50 lines each with
"Revolution"
	initcontainer("s",50,"Revolution",tab)	--> returns a list of 50 tab separated
'items' "Revolution"

To use your script, you should always pass p1,p2 and p3 (p3 only for numbers),
so initcontainer("n",50) should be called by
initcontainer("n",50,1,1,CR).  We assume defaultvalues for them.
The greatest part of my version of this function is only for argument checking,
and is only done once in the function.  Because this function is in a
stackinuse, it's not so awfully, I think.  (we've a library with functions like
this, so we avoid to re-wrrite a repeat loop every time again)

*) initcontainer("n",50,1,1,CR) returns a container from 1 to 51  in stead of
50.
*) put ... into line x of tResult : this is not correct : you should use a line
counter which is different from the loop variable x (try
initcontainer("n",50,101,2,comma)	), BUT appending each line to tResult is
*much* faster (and easier) than putting it into line x of tResult.  So "put
...&CR after lResult", and at the end remove the last CR would be faster.


>, but it eliminates a
>bunch of "do" statements (which will improve performance)

Sorry : in my script there was only ONE (=1) do statement  executed for each
call to initcontainer !!
I had
do "repeat ... end repeat" 

and not :
"repeat ... do... end repeat"

because doing it this way I avoid an extra test for each line in the repeat
loop :
    if pKind = "n" then add lStep to lValue

>and is a bit
>shorter as well...

shorter, yes   ;-)


Jan




More information about the use-livecode mailing list