Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!
which will replace str1 with str2. But what if I want to add str2 to str1, meaning I want to place str2 within str1, at any random point (including the beginning or the end of str1)?
put space & "despise" after char 1 of "I okra"
put "A" into char 2 of "XYZ"
put "hate" into word 2 of "I love okra"
put "any vegetable but okra" into word 3 of "I love okra"
Experiment. You can do this sort of thing with characters, words, items, lines, whatever. This is one of the most powerful tools in xTalk.
Thanks for the swift reply!
For some reason those examples won't work for me, the editor says "bad destination". I have to put the target strings into variables, e. g.
I gave incomplete examples. They were not "real", only ideas.
A variable is already somewhat "real". A literal string really cannot be considered so, and LC might bark at trying to do exactly what I wrote. I may have been cavalier.
Bet you that is the problem.
As for random stuff, try something like (and place all the destination strings into variables or fields first:
put "X" into any char of "123456"
put "dog" into any item of "cat,fish,parrot,velociraptor"
put "" into word 3 of "I love okra"
put "X" into char random(10) of "1234567890"
Craig
Last edited by dunbarx on Sun Apr 28, 2019 12:27 am, edited 1 time in total.
You must learn chunking. There are vast possibilities with them.
Consider that you can do something with the foundChunk, the mouseChunk, the selectedChunk. You already sort of know how this works if you ever asked for the number of words in a field. That is easy to understand, right?
These are all chunks, and can be manipulated in many ways. You just need time to assemble and dissassemble strings of text until it is second nature. I think the User Guide has quite a bit to say about chunks.
I will try to become a Chunkie .
I have played with all the examples and the randomness is working well with "any" or "random()", however the problem with "into" remains: it leads to replacing a piece of the target string. I am wondering though how to insert a string at a random point, without removing any of the target string.
redfield wrote: Sun Apr 28, 2019 8:03 pm
I will try to become a Chunkie
LOL!
redfield wrote: Sun Apr 28, 2019 8:03 pmI have played with all the examples and the randomness is working well with "any" or "random()", however the problem with "into" remains: it leads to replacing a piece of the target string.
INTO will always replace existing content!
redfield wrote: Sun Apr 28, 2019 8:03 pmI am wondering though how to insert a string at a random point, without removing any of the target string.
The answer has already been given earlier here, you need to put BEFORE or AFTER any random chunk like this:
...
put "1234" after any char of fld 1
put "whatever" before any word of fld 1
## and even:
put "1234" before any char of any word of fld 1
etc.
Try it, seeing is believing
Hope that helps!
Yes okay but somehow "before" and "after" to me don't result in complete random placement. For example if I put string1 "after" a random char in string2, it will never be placed at the very beginning of string2, because I determined it to be placed after some char. For complete randomness I would expect also the possibility of the string being placed at the very beginning of the target string. This could be achieved by using "before", but then again the string will never be placed at the very end of target string.
Add a dummy character after the original string. Put the new string before any character of the "extended" original string. Delete the last dummy character of the new combination string.
// whatever handler your trying to randomize in....
put "before" && "after" into tmpRandom
set the itemDelimiter to space
put any item of tmpRandom into tmpRandom
// now I'll borrow one of Klaus's examples....
put "1234" tmpRandom any char of fld 1
...
put random(2) into tRandomness
if tRandomness = 1 then
put "1234" BEFORE any char of any word of fld 1
else
put "1234" AFTER any char of any word of fld 1
end if
...
// whatever handler your trying to randomize in....
put "before" && "after" into tmpRandom
set the itemDelimiter to space
put any item of tmpRandom into tmpRandom
// now I'll borrow one of Klaus's examples....
put "put 1234" && tmpRandom && "any char of fld 2" into tmpStatement
do tmpStatement
Having said that though, Klaus's is certainly far easier. On the other hand, he really needs to practice his any statements