Page 1 of 1

Seperating a list by unique items [SOLVED]

Posted: Thu Jan 15, 2015 3:14 pm
by symiant
I'm trying to do something I'm sure is simple, but I can't find how to do it. I'm probably missing something!

I have a list in a variable, lets call it "StimList". The list has 80 lines, and each line has 3 items, all 3 items are words. For example lines 1-4 might look like:

file.bmp, file2.bmp, distinct
file3.bmp, file4.bmp, distinct
file5.bmp, file6.bmp, typical
file7.bmp, file8.bmp, typical

I'm trying to write some code that looks at each line, and puts that line into a new list based on the content of the third item (i.e. in this case distinct/typical). So the result would be two lists, one with all the distinct lines in and one list with all the typical lines in. However, I want to do use code that does this for all unique words as the third item, not just distinct and typical. So, for example, if the third line could contained one of three words, e.g. bird, cat, dog rather than two words e.g. distinct, typical. In case that's unclear, if I know in advance what the words in the third item were, I could use code that looks like something like this:

Code: Select all

repeat for each line thisline in StimList
if item 3 of thisline is distinct then put thisline after variable1
if item 3 of thisline is typical then put thisline after variable2
end repeat
But this would only work for the specific words distinct and typical as the third item. If I tried to use that on a list with different words, it wouldn't work. I think I need a way to put all unique values of the third item into its own list, then I can use that list to match to the items in StimList. How do I do this?

Thanks in advance!

Symiant

Re: Seperating a list by unique items

Posted: Thu Jan 15, 2015 3:37 pm
by magice
symiant wrote:I'm trying to do something I'm sure is simple, but I can't find how to do it. I'm probably missing something!

I have a list in a variable, lets call it "StimList". The list has 80 lines, and each line has 3 items, all 3 items are words. For example lines 1-4 might look like:

file.bmp, file2.bmp, distinct
file3.bmp, file4.bmp, distinct
file5.bmp, file6.bmp, typical
file7.bmp, file8.bmp, typical

I'm trying to write some code that looks at each line, and puts that line into a new list based on the content of the third item (i.e. in this case distinct/typical). So the result would be two lists, one with all the distinct lines in and one list with all the typical lines in. However, I want to do use code that does this for all unique words as the third item, not just distinct and typical. So, for example, if the third line could contained one of three words, e.g. bird, cat, dog rather than two words e.g. distinct, typical. In case that's unclear, if I know in advance what the words in the third item were, I could use code that looks like something like this:

Code: Select all

repeat for each line thisline in StimList
if item 3 of thisline is distinct then put thisline after variable1
if item 3 of thisline is typical then put thisline after variable2
end repeat
But this would only work for the specific words distinct and typical as the third item. If I tried to use that on a list with different words, it wouldn't work. I think I need a way to put all unique values of the third item into its own list, then I can use that list to match to the items in StimList. How do I do this?

Thanks in advance!

Symiant
I believe you are going to have to nest repeat loops, and compare the value of each third item to all of the other lines individually then, if it has not matched any of the lines (besides itself) save it after the appropriate variable. I know that that may be hard to follow. Unfortunately this is a case where actually writing the code is easier than explaining it but if I do that you will learn less. Please give it a try on your own first.

Re: Seperating a list by unique items

Posted: Thu Jan 15, 2015 4:29 pm
by Klaus
Hi Symiant,

1. you have some SPACES in your list and they will be part of your items!
item 3 of your first line = " distinct", note the SPACE at the beginning!
You should try to get rid of them :D

2. This is a wonderful case for an ARRAY!
This way you do not need to know every possible ITEM 3 of each line, so do something like this:

Code: Select all

...
put empty into tArray
repeat for each line tLine in Stimlist
  put tLine & CR after tArray[item 3 of tLine]
end repeat
...
Then you have an array tArray with every possible item 3 as the keys like this:
tArray["distinct"]
tArray["typical"]
tArray["another_possible_item3"]
etc...

To ge t list of all possible item 3s, do:
...
put the keys of tArray into tList_of_possible_item3s
...


Best

Klaus

Re: Seperating a list by unique items

Posted: Thu Jan 15, 2015 5:09 pm
by symiant
Ah thank you - I can see how both methods would do it!

I went with the array solution in the end, on the basis that I haven't used arrays much so this seems like a nice and simple first step :D

Thanks very much.