SOLVED: Nested Repeat Loop Problem

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

Post Reply
Xero
Posts: 157
Joined: Sat Jun 23, 2018 2:22 pm

SOLVED: Nested Repeat Loop Problem

Post by Xero » Fri Mar 28, 2025 2:54 pm

Hi Brainstrust,
Long time no hear.
I have a seemingly simple problem that I just can't get my head around how to script it.
I have a series of letters that I need to cycle through and get all of the permutations of it, and I just can't seem to get the right output.
I have shortened the lists for ease...
"B,A,P,Z,P,I,S,S,W"
"BI,AP,U,ZO,PA,IN,SC,SC,WO"
"BILT,APRI,P,ZOMB,PARA,SI,CRE,CRE,WO"

And I want get an output something like this:
BAP, BAZ, BAO, BAM, BAB, BAP, etc.
BPP, BPZ, BPO, BPM, etc.
BUA, BUP, BUR, BUI, BUZ, BUO, BUM, etc.

The process is this: take 1 item from the first list, then ignore the corresponding item in the other lists (item 1 in list 1, means ignore item 1 in all other lists). Take item 2 of list 2 and take the first character and ignore item 1 and 2 of list 3. Take item 3 of list 3 and take the first character, then the second character, then the third and fourth, then go back up one level, take character 2 of item 2 of list 2, and repeat with item 3 of list 3, etc.
Confusing, I know. It's brute force solving a game where you have a word list and need to write them out, so when you have used a word, you can't use it again. Then a certain letter from each word spells out another word. Literally 1000's of possibilities, so I thought an app that could output all of them could easily be scanned for the right answer.

The way I have been tackling it is to pick an item in list 1, then delete that item in all other lists, leaving me with one less item in all other lists, then picking an item of list 2, and deleting that item in all other lists, leaving it with less items. Eventually there will be one item in the last list. In the nested loops, the lists should be reinstated, but they aren't...

Here's what I have so far, but it's only cycling once...

Code: Select all

on mousedown
   put "B,A,P,Z,P,I,S,S,W" into wordList1
   put "BI,AP,U,ZO,PA,IN,SC,SC,WO" into wordList2
   put "BILT,APRI,P,ZOMB,PARA,SI,CRE,CRE,WO" into wordList3
   repeat with a = 1 to the number of items of wordList1
      put character 1 of item a of WordList1 into character1
      delete item a of WordList2
      delete item a of WordList3
      repeat with b = 1 to the number of items of WordList2
         repeat with z = 1 to the number of characters of item b of WordList2
            put character z of item b of WordList2 into character2
            delete item b of WordList3
            repeat with c = 1 to the number of items of WordList3
               repeat with y = 1 to the number of characters of item c of WordList3
                  put character y of item c of WordList3 into character3
                  put character1 & character2 & character3 into outputWord
                                                   put cr & outputWord after fld "Output"
                                                
                                             end repeat
                                          end repeat
                                       end repeat
                                    end repeat
                                 end repeat
 end mousedown
Which gives me a short list. It goes through once, does both of the last W & O, then stops. I think it's because I am deleting items out of lists in each step and when I repeat, they are no longer there.
Any thoughts om how to tackle the problem? I hope that's not too confusing, I can keep explaining if needed.
XdM
Last edited by Xero on Sat Mar 29, 2025 11:02 am, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10073
Joined: Fri Feb 19, 2010 10:17 am

Re: Nested Repeat Loop Problem

Post by richmond62 » Fri Mar 28, 2025 5:33 pm

Personally (and I know it would annoy a lot of people) I'd use 3 list fields . .

Xero
Posts: 157
Joined: Sat Jun 23, 2018 2:22 pm

Re: Nested Repeat Loop Problem

Post by Xero » Fri Mar 28, 2025 5:39 pm

richmond62 wrote:
Fri Mar 28, 2025 5:33 pm
Personally (and I know it would annoy a lot of people) I'd use 3 list fields . .
Not sure how that will help the problem at hand... Happy to take advice about how that might help though!
XdM

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10073
Joined: Fri Feb 19, 2010 10:17 am

Re: Nested Repeat Loop Problem

Post by richmond62 » Fri Mar 28, 2025 6:54 pm

Busy this evening (Eastern Europe): will have a go tomorrow.

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

Re: Nested Repeat Loop Problem

Post by dunbarx » Sat Mar 29, 2025 1:02 am

Xero. I think I see what you are after, but do not understand your example. From your list, I can see how you would find "BAP". But do not see how you got "BAZ", because that is not a word, at least not in either the English or American lexicons. Further, I cannot see how you got "BAO", even though that word is indeed in the English lexicon, but there is no free "O" from which to grab it.

A method of collecting all the combinations of letters from a list of strings is certainly doable, but I do not know the rules you have in mind in which to proceed.

Craig

Xero
Posts: 157
Joined: Sat Jun 23, 2018 2:22 pm

Re: Nested Repeat Loop Problem

Post by Xero » Sat Mar 29, 2025 4:30 am

I think I have managed to solve it. It's just chugging out a lot of stuff right now and is not responding. When it finishes, I will have a better idea. I have just tracked the changing word lists at each level, and reinstated them as I loop back up the repeats.
Basically, it will chug out a list of random letter strings that would need to be checked to see if they are English words. I was planning on doing it manually by deleting swathes of responses due to them not starting with an understandable English letter combination, then eyeballing the rest.
If there is a way of checking to see if the output are English words, it would speed up the process!
The method of production is thus:
List 1, item 1, char 1 : List 2, Item 2, char 1: List 3, Item 3, char 1. (list 2 doesn't use item 1, because list one uses that corresponding item, etc)
List 1, item 1, char 1: List 2, item 2, char 1: List 3, Item 3, char 2.
List 1, item 1, char 1: List 2, item 2, char 1: List 3, Item 3, char 3. etc for list 3's item
List 1, item 1, char 1: List 2, Item 2, char 2: List 3, item 3, char 1. etc.
I hope that explains the rules. It's a bit weird and hard to explain. At each line, I need to run through all of the possibilities of each set of letters.
XdM

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10073
Joined: Fri Feb 19, 2010 10:17 am

Re: Nested Repeat Loop Problem

Post by richmond62 » Sat Mar 29, 2025 8:47 am

Here I am at last: and at about 3 o'clock in the morning (you have a lot to answer for :lol: ) I was tossing and turning sure that there was something 'a bit odd' about your script.

Whether it was your script that was a bit odd, or the state my brain was in at 3 am has yet to be seen . . .

Notwithstanding; I shall stick my head under the guillotine at the risk of . . .

I shall repost your script here, but NOT in a script box so I can highlight the bits that made me uneasy:
-----------------------------------
on mousedown
put "B,A,P,Z,P,I,S,S,W" into wordList1
put "BI,AP,U,ZO,PA,IN,SC,SC,WO" into wordList2
put "BILT,APRI,P,ZOMB,PARA,SI,CRE,CRE,WO" into wordList3
repeat with a = 1 to the number of items of wordList1
put character 1 of item a of WordList1 into character1
delete item a of WordList2
delete item a of WordList3

repeat with b = 1 to the number of items of WordList2
repeat with z = 1 to the number of characters of item b of WordList2
put character z of item b of WordList2 into character2
delete item b of WordList3
repeat with c = 1 to the number of items of WordList3
repeat with y = 1 to the number of characters of item c of WordList3
put character y of item c of WordList3 into character3
put character1 & character2 & character3 into outputWord
put cr & outputWord after fld "Output"

end repeat
end repeat
end repeat
end repeat
end repeat
end mousedown
-----------------------------------

It is the nesting that fusses me . . .

It seems to me that, having entered your words/chunks into a variable, at each iteration of the loop your variables get shorter and shorter, when what is MEANT to happen in WordList2 and WordList3 only a single item has to be removed each time.
Last edited by richmond62 on Sat Mar 29, 2025 7:32 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10073
Joined: Fri Feb 19, 2010 10:17 am

Re: Nested Repeat Loop Problem

Post by richmond62 » Sat Mar 29, 2025 8:55 am

The first thing I am going to do is exchange that mouseDown for a mouseUp (thank you, Klaus, 2002).

This will make sure the script does not fire more than once because a finger was not removed quickly enough from a mouse button.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10073
Joined: Fri Feb 19, 2010 10:17 am

Re: Nested Repeat Loop Problem

Post by richmond62 » Sat Mar 29, 2025 9:11 am

For what its worth:
-
Screenshot 2025-03-29 at 10.09.13.png
-
The green button runs your script, the blue button runs mine. 8)
Attachments
Little Birds in Their Nests.livecode.zip
Stack.
(1.44 KiB) Downloaded 184 times

Xero
Posts: 157
Joined: Sat Jun 23, 2018 2:22 pm

Re: Nested Repeat Loop Problem

Post by Xero » Sat Mar 29, 2025 11:02 am

richmond62 wrote:
Sat Mar 29, 2025 9:11 am
For what its worth:
-
Screenshot 2025-03-29 at 10.09.13.png
-
The green button runs your script, the blue button runs mine. 8)
Sorry for keeping you up at night Richmond! I can see what you did there in terms of resetting the wordlist 2 and 3 inside the first loop. It was something that came to me at 4:35am if it is any consolation to you!
And I must make an apology to Klaus. After saying many times not to mouseDown, I still default to mouseDown! Old habits die hard. One of these days I will fall into best practice...
it looks like we have solved it though.
XdM

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10073
Joined: Fri Feb 19, 2010 10:17 am

Re: SOLVED: Nested Repeat Loop Problem

Post by richmond62 » Sat Mar 29, 2025 11:44 am

Well, at least my sadistic streak is satisfied, knowing you had a dark night of the soul as well. 8)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10073
Joined: Fri Feb 19, 2010 10:17 am

Re: SOLVED: Nested Repeat Loop Problem

Post by richmond62 » Sun Mar 30, 2025 3:00 pm

Of course 'things' would work much more quickly if those lists were NOT stored in fields, but in custom properties of the button.

Post Reply