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

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 8:31 pm

Nah... no winners or losers. Just a bit of fun, but a kind of game while mentioning something useful to the op. Maybe.

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 9:05 pm

I think we are all winners in this, but your right about it being a fun game :D
Image

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

Re: Insert string into string at random position

Post by redfield » Tue Apr 30, 2019 10:07 pm

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

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 » Tue Apr 30, 2019 10:22 pm

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

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

Re: Insert string into string at random position

Post by redfield » Tue Apr 30, 2019 10:34 pm

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.

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

Re: Insert string into string at random position

Post by Klaus » Tue Apr 30, 2019 10:46 pm

redfield wrote:
Tue Apr 30, 2019 10:34 pm
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.
We already solved your problem on the first page!

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

Re: Insert string into string at random position

Post by redfield » Tue Apr 30, 2019 10:50 pm

Klaus wrote:
Tue Apr 30, 2019 10:46 pm
redfield wrote:
Tue Apr 30, 2019 10:34 pm
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.
We already solved your problem on the first page!
No doubt on that! I was replying to Craig's last post :)

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

Re: Insert string into string at random position

Post by Klaus » Tue Apr 30, 2019 11:24 pm

redfield wrote:
Tue Apr 30, 2019 10:50 pm
Klaus wrote:
Tue Apr 30, 2019 10:46 pm
redfield wrote:
Tue Apr 30, 2019 10:34 pm
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.
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. :oops:

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 » Wed May 01, 2019 3:09 am

Hi.

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"
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

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 » Wed May 01, 2019 3:22 am

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

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

Re: Insert string into string at random position

Post by SparkOut » Wed May 01, 2019 7:33 am

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.

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 » Wed May 01, 2019 2:56 pm

@ Sparkout.
Erm, if you put the + 1 inside the parentheses ...
Yep. Cleaner.

@ Redfield. See? There are always fun things to do in LC.

Craig

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

Re: Insert string into string at random position

Post by redfield » Fri May 03, 2019 9:31 pm

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

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 May 05, 2019 12:13 am

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

Post Reply