Position of character in a for-each loop

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

JoeE.
Posts: 12
Joined: Tue Apr 27, 2010 5:33 pm

Re: Position of character in a for-each loop

Post by JoeE. » Fri Jul 08, 2022 8:12 am

Hi,

what I want to achieve is to have combinations of all items from all fields, but in ascending order. But since these aren't numeric values, I thought the easiest way would be to use the loop position for that purpose. So, your examples are good and work fine for me!

Image

This forum is very helpful. Thanks a lot for this! :D

Best regards,
Joerg

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Position of character in a for-each loop

Post by dunbarx » Fri Jul 08, 2022 1:29 pm

Joerg.

OK, that is very different from what I though you wanted earlier. Did you write a routine to do that? If not, it would be fun to do so.

Craig

JoeE.
Posts: 12
Joined: Tue Apr 27, 2010 5:33 pm

Re: Position of character in a for-each loop

Post by JoeE. » Sat Jul 09, 2022 8:37 am

you are right, sorry for the confusion! No, the screenshots are fake :wink: For the version on the left I would simply use 3 nested for-each loops. The version on the right, I think, needs to compare lower and higher values to achieve the ascending order. That's why I asked the opening question, just to keep it simple...

Best regards,
Joerg

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Position of character in a for-each loop

Post by richmond62 » Sat Jul 09, 2022 8:45 am

I have feeling that ALL you need to do with your listField on the right is use SORT.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Position of character in a for-each loop

Post by dunbarx » Sat Jul 09, 2022 1:40 pm

@Richmond.

If I have the OP's intent correctly, he wants all possible ascending three-item strings extracted from the "master" field contents. Sorting doesn't do that at all.

@Jeorg.

Is that true? And again, if so, it would be a simple but fun project to write a function that does that sort of thing, and of any length, not just three.

And I assume you might want those extracted strings to be derived from "master" strings that are not already sorted, as, for example, "B,D,A,E,C"

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Position of character in a for-each loop

Post by dunbarx » Sat Jul 09, 2022 3:19 pm

Could not resist.

Here ia a stack that takes any list of items and finds the strings I believe you are seeking. The button has it all. You can make your own item list in the top field, and create strings of any length.
Attachments
Item extractor.livecode.zip
(1.19 KiB) Downloaded 80 times
Last edited by dunbarx on Sat Jul 09, 2022 11:14 pm, edited 5 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Position of character in a for-each loop

Post by dunbarx » Sat Jul 09, 2022 11:10 pm

reorg.

The slightly updated stack I posted sorts the items of the "master" list. It occurs to me that this may not be desirable at all. Losing the "sort" line makes it go away.

Craig

JoeE.
Posts: 12
Joined: Tue Apr 27, 2010 5:33 pm

Re: Position of character in a for-each loop

Post by JoeE. » Mon Jul 11, 2022 5:19 pm

dunbarx wrote:
Sat Jul 09, 2022 1:40 pm
@Richmond.

If I have the OP's intent correctly, he wants all possible ascending three-item strings extracted from the "master" field contents. Sorting doesn't do that at all.

@Jeorg.

Is that true? And again, if so, it would be a simple but fun project to write a function that does that sort of thing, and of any length, not just three.

And I assume you might want those extracted strings to be derived from "master" strings that are not already sorted, as, for example, "B,D,A,E,C"

Craig
Craig, thanks for the stack. This works fine :D. How would you do it with 3 input fields? Using nested loops? I have a feeling nested loops will eventually end up like gotos :cry:

Best,
Joerg

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Position of character in a for-each loop

Post by dunbarx » Mon Jul 11, 2022 5:31 pm

Joe.
How would you do it with 3 input fields? Using nested loops?
You certainly are a fan of nested loops. :D

Whatever method you use to create the desired item list has to come from a single dataSet, no? In other words, are you asking how to merge the three fields?

Or do I not understand?

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Position of character in a for-each loop

Post by dunbarx » Mon Jul 11, 2022 5:38 pm

Joe.

The sample stack automatically sorts the "master" list of items. Did you see that in the following post I mentioned this may be something nobody wants? Anyway, maybe an option should be available.

I mention this again because if you want to merge three fields, how would you like that done? Create a single dataSet with the three field contents strung out as written? Merged and sorted? in either case we have to make sure that the contents of each field are assembled with commas placed properly. This in order to preserve the number of items in each field after they are put together.

Can you give an example of those three fields and what you are looking for?

Craig

JoeE.
Posts: 12
Joined: Tue Apr 27, 2010 5:33 pm

Re: Position of character in a for-each loop

Post by JoeE. » Mon Jul 11, 2022 5:47 pm

...I'm not really a fan of nested loops, but it's quite comfortable :wink:

Don't worry too much about my little problem, I think we are moving too far away from the opening question. All I want is to have all combinations of the items of 3 fields in an ascending order, no matter what kind they are. Still items like: L,I,V,E,C,O,D,E should be treated as 1,2,3,4,5,6,7,8.

I'm not very good at explaining, sorry for that :(

Joerg

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Position of character in a for-each loop

Post by dunbarx » Mon Jul 11, 2022 6:21 pm

Joe.
All I want is to have all combinations of the items of 3 fields in an ascending order, no matter what kind they are.
Do you mean then that each of the three fields should be treated separately, with three separate outputs? And NOT that the contents of those three fields are combined in any way before processing? If so then you can take advantage of your nested loop addiction.
Item extractor2.livecode.zip
(1.56 KiB) Downloaded 75 times
And remember, the longer I work with you, the less time I have to spend in my day job. :wink:

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”