When building a list, is it possible to segment it as you build?

Hello, I’m just trying to understand the finer points of list building, and retain some semblance of sanity :slight_smile:

My routine is supposed to take a list formatted as {1,3,5,7} and convert it to {{1}, {1, 2, 3}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5, 6, 7}}
Unfortunately, at the moment I am getting this instead: {1, 1, 2, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7, …}
If I need it to be: {{1}, {1, 2, 3}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5, 6, 7}} is it even possible to create it this way on the fly?

Thanks for the clarification!

Here’s the routine:

set working_list to {{{1}, {3}, {5}, {7}}, {{2}, {4}, {6}, {8}}, {{9}, {10}, {11}, {12}}} --contains multiple groups of four each from 1 to n

–item 1 of working_list contains {1}
–item 2 of working_list contains {3}
–item 3 of working_list contains {5}
–item 4 of working_list contains {7}

count working_list --indicates 3 groups of four will be processed

set the_new_item to {} --to contain the processed list

repeat with i from 1 to count working_list --counts 3 times, processing three groups of 4
set sub_working_list to item i of working_list --{1}, get first item in group of {{1}, {3}, {5}, {7}}

repeat with y from 1 to count sub_working_list --Repeat 4 times (four values in a set)
	set the_item to item y of sub_working_list --the item = {1} on the first pass
	
	repeat with x from 1 to the_item as integer --repeat as many times as the literal value: first pass=1 then 2nd=3, 3rd=5, 4rth=7
		set the_new_item to the_new_item & x --build up the list
		display dialog the_new_item as string --check the work
	end repeat
	
end repeat

end repeat

Does this work?:

on processPart(l)
    set c to count of l
    repeat with i from 1 to c
        set li to item i of l
        if class of li is list then
            processPart(li)
        else if class of li is integer then
            set s to {}
            repeat with j from 1 to li
                set end of s to j
            end repeat
            set item i of l to s
        end if
    end repeat
end processPart

-- On Run
set l to {{{1}, {3}, {5}, {7}}, {{2}, {4}, {6}, {8}}, {{9}, {10}, {11}, {12}}}

processPart(l)

return l

BTW, I wonder if you wanted the input list to be the following instead:

{{1, 3, 5, 7}, {2, 4, 6, 8}, {9, 10, 11, 12}}

And I should ask: what is this being used for? Because it can probably be made more efficient, so that it calculates the value you want on the fly, rather than having a list stored in memory.

Yes, I wonder if I am making the whole thing more complex than it needs to be!

The script looks at a directory of files, sorting them from highest to lowest… the files are named with the number of pages contained within them, so the first four might look like this:

10.pdf, 8.pdf, 7.pdf, 3.pdf

Ultimately they are all going to print on the same sheet in “lanes” from left to right. Each column will print from top to bottom sequentially and so there will be blanks in some of the columns. Anyway, the next routine will grab the sorted list and divide it into groups of four. ( If the list is not evenly divisible by 4, I need to figure out yet what to do with the remaining files…)

At this point, the sorted list looks like this: {{10, 8, 7, 3}, {list of the next four numbers}, etc.}

For the first group of four (item 1 of the list), what I want to process looks like this:

1 1 1 1 sheet 1

2 2 2 2 sheet 2

3 3 3 3 …
4 4 4 blank
5 5 5 blank
6 6 6 blank
7 7 7 blank
8 8 blank blank
9 blank blank blank
10 blank blank blank sheet 10

I loop through each row writing some test to a file. At the end of the block above, the file is written, saved to disk and the process repeats with the next group of four. At the end of the day, there is one text file produced for every group of 4 numbers in the original sorted list.