Insert string into string at random position
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: Insert string into string at random position
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
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
Thanks guys for your interesting suggestions. I had the same idea as Klaus and meanwhile created this micro function:
And then put it into an if statement. Great to know that I'm on the right track 
Code: Select all
function dice
return random(2)
end dice

Re: Insert string into string at random position
Hi.
After all the stuff, have you simply:
And to get much cuter, if you do not know the length of temp:
Craig
After all the stuff, have you simply:
Code: Select all
put "abcdefgh" into temp
put "X" after char random(9) of temp
Code: Select all
put "X" after char random(the length of someTextofInterest) of someTextofInterest
Re: Insert string into string at random position
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
We already solved your problem on the first page!
Re: Insert string into string at random position
Re: Insert string into string at random position
Re: Insert string into string at random position
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:
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
Is the issue that the "random" function, as it stands, will not return a "O"? So that you can never:
Code: Select all
put "X" after char 0 of yourText"
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
Sorry, Jacque, cannot resist.
Craig
Re: Insert string into string at random position
I can feel Jacque's glare.
OK, OK. Fine. Make a field 1 somewhere. Put this in a button script.
Sheesh.
Craig
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
Craig
Re: Insert string into string at random position
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
@ Sparkout.
@ Redfield. See? There are always fun things to do in LC.
Craig
Yep. Cleaner.Erm, if you put the + 1 inside the parentheses ...
@ Redfield. See? There are always fun things to do in LC.
Craig
Re: Insert string into string at random position
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
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
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:
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
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
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