how can I make nested lists work consistently?

Here is my function:

to handle SaveReport reportFile, resultList
Log resultList
put the number of items in the first item of resultList into count
subtract 1 from count
put () into newLine

if file reportFile does not exist then
create file reportFile
open file reportFile for writing
open file reportFile for appending
end if

repeat count times
put repeatIndex() into loopcount
repeat for each valueList in resultList
insert item 1 of valueList after newLine
put "," after newLine
put item loopcount + 1of valueList after newLine
combine newLine
put CarriageReturn after newLine
write newLine to file reportFile
put () into newLine
end repeat
write CarriageReturn to file reportFile
end repeat
close file reportFile
end SaveReport

Data Set 1:
put "testfile.csv" into myFile
put (("Build", "Trunk 21321"), ("Login Test", 21.54), ("Event Test", 52.3)) into myList

Result Set 1:
Build,Trunk 21321
Login Test,21.54
Event Test,52.3

Data Set 2:
put ("Build", "Thar 23545") into the first item of myList
insert ("D&D Hostile", 23.56) nested after myList
insert ("Report Button", 17.35) nested into myList

Result Set 2:
(Build,Thar 23545)
D&D Hostile,23.56
Report Button,17.35

If I log out either data set before I run the SaveReport function, the results look the same, but the function is somehow treating them differently. Can someone please explain.


  • EggplantMattEggplantMatt ForumAdmin admin
    The problem is here:
    put ("Build", "Thar 23545") into the first item of myList
    The myList variable is empty prior to running this line of code, so it's not a list. Doing "put into" puts the specified value as a text item because it is going into a text container. So it looks like you're putting a list into a variable, but you're really putting a string that starts and ends with parentheses into the variable. If right after running this code you ran these lines:
    put myList
    put myList is a list
    put the first item of myList is a list
    The output would be:
    (Build,Thar 23545)
    So at this point there is no list (note that outputting myList has only a single set of parentheses, not the double parentheses of a list within a list). There are two ways of fixing this: The first is to initialize your myList variable as a list:
    put () into myList
    put ("Build", "Thar 23545") into the first item of myList
    Or, alternatively, you can specify that you want to put a list item into the variable:
    put ("Build", "Thar 23545") into the first list item of myList
    Either of these will give you a list and the rest of the code will work as expected.

    As for the rest of your code, unless I'm missing something, your whole handler can be rewritten as this:
    to handle SaveReport reportFile, resultList
    	repeat for each valueList in resultList
    		combine valueList
    		put valueList & return after file reportFile
    	end repeat
    end SaveReport
    You don't need to check for the file -- if it's not there, eggPlant will create it for you. There's no need to open the file for reading or appending; eggPlant will handle that automatically. Since you're always putting the value at the end of the file, you don't need to worry about whether it's open for reading or appending -- "put...after" will do the right thing.

    You were initializing a variable to a list and then implicitly converting it to a string before writing it out -- there was a lot of effort going into what seemed to be just getting the value you started with. Note also that "newline" is actually a SenseTalk keyword, synonymous with "return"; you're allowed to overwrite it with whatever value you'd like, but I thought I'd point out that you were doing it.

    It may be that your actual data is more complex than the example you provided and that you need some additional logic to get the output that you want. If that's the case and you want to provide a sample of your actual data along with a description of the processing that you're trying to perform, we'd be happy to suggest an appropriate solution.
Sign In or Register to comment.