Page 1 of 2
Insert string into string at random position
Posted: Sat Apr 27, 2019 10:33 pm
by redfield
Hi
as far as I have understood, I can do a
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)?
Example:
str1: abcd
str2: xyz
abcd -> abcxyzd
or
abcd -> xyzabcd
Re: Insert string into string at random position
Posted: Sat Apr 27, 2019 10:41 pm
by dunbarx
HI.
You have entered the world of chunks. Putting text INTO a string does just as you said.
Now try putting some text AFTER or BEFORE a string.
Now get really fancy and:
Code: Select all
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.
Craig Newman
Re: Insert string into string at random position
Posted: Sat Apr 27, 2019 11:04 pm
by redfield
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.
Code: Select all
put "I okra" into myvar
put space & "despise" after char 1 of myvar
Then they work fine. However in your examples the target positions are all specified, but I am wondering how to position str2 randomly?
Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 12:10 am
by dunbarx
Ah,
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:
Code: Select all
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
Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 12:26 am
by dunbarx
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.
Craig
Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 8:03 pm
by redfield
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.

Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 8:12 pm
by Klaus
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 pm
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.
INTO will always replace existing content!
redfield wrote: ↑Sun Apr 28, 2019 8:03 pm
I 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!
Best
Klaus
Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 8:59 pm
by redfield
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.

Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 10:19 pm
by SparkOut
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.
Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 10:20 pm
by bogs
Well, you *can* randomize it even more by sticking the choice of 'before' or 'after' into random too. For example, something like (psuedo) -
Code: Select all
// 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
Try it and let us know how it flies.
Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 10:22 pm
by Klaus
OK, agreed...
Hm, what about this randomness:
Code: Select all
...
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
...

Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 10:25 pm
by Klaus
bogs wrote: ↑Sun Apr 28, 2019 10:20 pm
Try it and let us know how it flies.
Not very high actually
-> Error at line 15 (Handler: can't find handler) near "tmpRandom", char 8
You will need to
DO this somehow.
Re: Insert string into string at random position
Posted: Sun Apr 28, 2019 10:40 pm
by bogs
Hey, that was out of what few brain cells I had firing at the time
Ok, here is one that works for sure (TESTED)

- ...and CONFIRMED...
- Selection_001.png (9.72 KiB) Viewed 9488 times
Code: Select all
// 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

Re: Insert string into string at random position
Posted: Mon Apr 29, 2019 6:50 pm
by SparkOut
In defence of Klausimausi

, there's also an opportunity to practice chunking delimiters
What's wrong with
and not have to worry about the delimiter, or
Code: Select all
put any word of tmpRandom into tmpRandom
and not have to worry about the delimiter?

Re: Insert string into string at random position
Posted: Mon Apr 29, 2019 8:08 pm
by bogs
I could say you win, but I think that is a given
