repeat string: how ?
Jan Decroos
jan.decroos at groepvanroey.be
Wed Nov 20 04:59:01 EST 2002
use-revolution at lists.runrev.com writes:
>
>Actually, when I originally wrote it, I was not counting on having p1, p2
>and p3 filled, but I failed to change the lines that said "put p1 into
>pStartNum" to "if p1 is not empty then put p1 into pStartNum. The function
>should have been:
>
>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
> put cr into tSeparator
> switch pKind
> case "n"
> if p1 is not empty then put p1 into pStartNum
> if p2 is not empty then put p2 into tNumSteps
> if p3 is not empty then put p3 into tSeparator
> break
> case "s"
> put p1 into tString
> if p2 is not empty then 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
>
>In this case, the only things necessary are pKind and pCount (for numbers)
>and pKind, pCount and p1 (for strings).
Agree, BUT
now you can't have an empty separator !
(was the original question by alrice at swcp.com :
"fu" x 20 => fufufufufufufufufufufufufufufufufufufufu
)
my InitContainer("s",3,"a","") --> "aaa"
your InitContainer("s",3,"a","") --> "a"&CR&"a"&CR&"a"&CR&"a" :
- CR as separator in stead of empty
- 4 lines because (see earlier: x is not the destination line number (use a
2nd variable, or better use "put ...&CR after tResult"))
- In the repeat, there should be an 'else if' in stead of the 2nd 'if', to
avoid unnecessary tests.
I did some speed tests, and asuming your InitConatiner returns the exact
result, here are the results :
(called your function "initContainer2")
on mouseUp
put the milliseconds into ms
put initContainer("s",10000,"aaaa") into p
put the milliseconds - ms into lRes
put the milliseconds into ms
put initContainer2("s",10000,"aaaa") into p
put space&(the milliseconds - ms) after lRes
put the milliseconds into ms
put initContainer("n",10000) into p
put space&(the milliseconds - ms) after lRes
put the milliseconds into ms
put initContainer2("n",10000) into p
put space&(the milliseconds - ms) after lRes
put lRes
end mouseUp
lRes : (In the msg box :) 20 6054 473 6380 (20 (my result) <-> 6054 (your
result) and 473 (my result) <> 6380 (your result) !!!)
The first part of my initContainer (argument checking) takes .... zero to one
milliseconds !!
Jan
More information about the use-livecode
mailing list