Insert string into string at random position

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!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Insert string into string at random position

Post by redfield » Sat Apr 27, 2019 10:33 pm

Hi :D
as far as I have understood, I can do a

Code: Select all

put str2 into str1
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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Insert string into string at random position

Post by dunbarx » Sat Apr 27, 2019 10:41 pm

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

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Insert string into string at random position

Post by redfield » Sat Apr 27, 2019 11:04 pm

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?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Insert string into string at random position

Post by dunbarx » Sun Apr 28, 2019 12:10 am

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
Last edited by dunbarx on Sun Apr 28, 2019 12:27 am, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Insert string into string at random position

Post by dunbarx » Sun Apr 28, 2019 12:26 am

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

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Insert string into string at random position

Post by redfield » Sun Apr 28, 2019 8:03 pm

I will try to become a Chunkie :D .
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. :(

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Insert string into string at random position

Post by Klaus » Sun Apr 28, 2019 8:12 pm

redfield wrote:
Sun Apr 28, 2019 8:03 pm
I will try to become a Chunkie :D
LOL! :D :D :D
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

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Insert string into string at random position

Post by redfield » Sun Apr 28, 2019 8:59 pm

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. :|

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Insert string into string at random position

Post by SparkOut » Sun Apr 28, 2019 10:19 pm

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.

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Insert string into string at random position

Post by bogs » Sun Apr 28, 2019 10:20 pm

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.
Image

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Insert string into string at random position

Post by Klaus » Sun Apr 28, 2019 10:22 pm

OK, agreed... 8)

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
...
:D

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Insert string into string at random position

Post by Klaus » Sun Apr 28, 2019 10:25 pm

bogs wrote:
Sun Apr 28, 2019 10:20 pm
Try it and let us know how it flies.
Not very high actually :D
-> Error at line 15 (Handler: can't find handler) near "tmpRandom", char 8

You will need to DO this somehow.

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Insert string into string at random position

Post by bogs » Sun Apr 28, 2019 10:40 pm

Hey, that was out of what few brain cells I had firing at the time :P

Ok, here is one that works for sure (TESTED)
Selection_001.png
...and CONFIRMED...
Selection_001.png (9.72 KiB) Viewed 9481 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 :wink:
Image

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Insert string into string at random position

Post by SparkOut » Mon Apr 29, 2019 6:50 pm

In defence of Klausimausi :lol: , there's also an opportunity to practice chunking delimiters :wink:
What's wrong with

Code: Select all

put "before,after" into tmpRandom
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?
8) :lol:

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Insert string into string at random position

Post by bogs » Mon Apr 29, 2019 8:08 pm

I could say you win, but I think that is a given :D
Image

Post Reply