Page 2 of 2
Re: Insert string into string at random position
Posted: Mon Apr 29, 2019 8:31 pm
by SparkOut
Nah... no winners or losers. Just a bit of fun, but a kind of game while mentioning something useful to the op. Maybe.
Re: Insert string into string at random position
Posted: Mon Apr 29, 2019 9:05 pm
by bogs
I think we are all winners in this, but your right about it being a fun game

Re: Insert string into string at random position
Posted: Tue Apr 30, 2019 10:07 pm
by redfield
Thanks guys for your interesting suggestions. I had the same idea as Klaus and meanwhile created this micro function:
Code: Select all
function dice
return random(2)
end dice
And then put it into an if statement. Great to know that I'm on the right track

Re: Insert string into string at random position
Posted: Tue Apr 30, 2019 10:22 pm
by dunbarx
Hi.
After all the stuff, have you simply:
Code: Select all
put "abcdefgh" into temp
put "X" after char random(9) of temp
And to get much cuter, if you do not know the length of temp:
Code: Select all
put "X" after char random(the length of someTextofInterest) of someTextofInterest
Craig
Re: Insert string into string at random position
Posted: Tue Apr 30, 2019 10:34 pm
by redfield
Yes but here again the problem is, that "X" will never be placed at the very beginning of the target string. I am looking for a solution that includes a possible placement at the beginning or the very end of the target string.
Re: Insert string into string at random position
Posted: Tue Apr 30, 2019 10:46 pm
by Klaus
redfield wrote: Tue Apr 30, 2019 10:34 pmYes but here again the problem is, that "X" will never be placed at the
very beginning of the target string. I am looking for a solution that includes a possible placement at the beginning or the very end of the target string.
We already solved your problem on the first page!
Re: Insert string into string at random position
Posted: Tue Apr 30, 2019 10:50 pm
by redfield
Klaus wrote: Tue Apr 30, 2019 10:46 pm
redfield wrote: Tue Apr 30, 2019 10:34 pmYes but here again the problem is, that "X" will never be placed at the
very beginning of the target string. I am looking for a solution that includes a possible placement at the beginning or the very end of the target string.
We already solved your problem on the first page!
No doubt on that! I was replying to Craig's last post

Re: Insert string into string at random position
Posted: Tue Apr 30, 2019 11:24 pm
by Klaus
redfield wrote: Tue Apr 30, 2019 10:50 pmKlaus wrote: Tue Apr 30, 2019 10:46 pmredfield wrote: Tue Apr 30, 2019 10:34 pmYes but here again the problem is, that "X" will never be placed at the
very beginning of the target string. I am looking for a solution that includes a possible placement at the beginning or the very end of the target string.
We already solved your problem on the first page!
No doubt on that! I was replying to Craig's last post

Oops, sorry for that.

Re: Insert string into string at random position
Posted: Wed May 01, 2019 3:09 am
by dunbarx
Hi.
Is the issue that the "random" function, as it stands, will not return a "O"? So that you can never:
But that is a minor issue, and one that we all deal with daily. You sometimes have to tweak the native tongue:
Code: Select all
on mouseUp
put "abcde" into temp
put "X" after char random(the length of temp) - 1 of temp
put temp into fld 1
end mouseUp
Now this solves the problem of placing the "X" at the very beginning, but has its own issue. Do you see what that is? Do you see how to fix it? Sometimes one tweak needs another.
Sorry, Jacque, cannot resist.
Craig
Re: Insert string into string at random position
Posted: Wed May 01, 2019 3:22 am
by dunbarx
I can feel Jacque's glare.
OK, OK. Fine. Make a field 1 somewhere. Put this in a button script.
Code: Select all
on mouseUp
put "abcde" into temp
put random(the length of temp) + 1 into index
if index - the length of temp = 1 then put "0" into index
put "X" after char index of temp
put temp into fld 1
end mouseUp
Sheesh.
Craig
Re: Insert string into string at random position
Posted: Wed May 01, 2019 7:33 am
by SparkOut
Erm, if you put the + 1 inside the parentheses so that the random pool is enlarged, then you can just subtract 1 from the index and not have to shift the one to zero.
Re: Insert string into string at random position
Posted: Wed May 01, 2019 2:56 pm
by dunbarx
@ Sparkout.
Erm, if you put the + 1 inside the parentheses ...
Yep. Cleaner.
@ Redfield. See? There are always fun things to do in LC.
Craig
Re: Insert string into string at random position
Posted: Fri May 03, 2019 9:31 pm
by redfield
Hi guys,
thanks again for the enlightment - I didn't realize that one can work with -> char 0 <-. So the latter suggestion seems to be a great solution

Re: Insert string into string at random position
Posted: Sun May 05, 2019 12:13 am
by dunbarx
Hi.
Yep, putting a char after char 0 of a string is the same as putting before the string.
Know also that you can put a char after a negative value. This does not place it on your knees, it places it to the left of the end of the string. So:
Code: Select all
put "abcdefg" into temp
put "X" after char - 3 of temp
gives you "abcdeXfg"
If you go overboard with this, like putting "X" into -250 of that string, it places it at the beginning. Similarly, if you put "X" after char 250 of that string, it places it at the end.
Craig