Re: Convert any list to a table?
Re: Convert any list to a table?
- Subject: Re: Convert any list to a table?
- From: Nigel Garvey <email@hidden>
- Date: Wed, 19 Sep 2001 23:52:47 +0100
Jason Bourque wrote on Tue, 18 Sep 2001 22:53:53 -0400:
>
Ok, before I pull out any hair.
>
>
--So how do you create a false table with tabs from a list and
>
--not have an extra character at the end. Plus handle even and odd lists?
>
--And handle any number of columns.
This isn't very nice, but it works in the way I think you want - ie. all
empty space is in the last row rather than in the last column. Basically
it's your handler, with modifications to the formulae for calculating the
number of rows needed and for finding the next item in each row. The
returns are inserted *after* the inner repeat.
on createFalseTable(anyList, columnsNeeded)
set anyListCount to count of anyList
set rowsNeeded to (anyListCount div columnsNeeded)
set lastRowLen to (anyListCount mod columnsNeeded)
if lastRowLen is 0 then -- every row will be filled
set lastRowLen to columnsNeeded
else -- an extra, partial row is needed
set rowsNeeded to rowsNeeded + 1
end if
set newList to {}
repeat with rowsNth from 1 to rowsNeeded
repeat with columnNth from 1 to columnsNeeded
try
if columnNth is not greater than lastRowLen then
-- Use a formula based on the full number of rows
item ((columnNth - 1) * rowsNeeded + rowsNth) of anyList
else if rowsNth is not rowsNeeded then
-- Use a formula based on the number of full rows,
-- starting after the full columns
item (lastRowLen * rowsNeeded + (columnNth - lastRowLen - 1)
* (rowsNeeded - 1) + rowsNth) of anyList
end if
set end of newList to result
set end of newList to tab
on error
---
end try
end repeat
-- Replace the most recent tab with a return
set item -1 of newList to return
end repeat
items 1 thru -2 of newList as string
end createFalseTable
set anyList to {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh",
"iii", "jjj", "kkk"}
my createFalseTable(anyList, 5)
--> "aaa ddd fff hhh jjj
bbb eee ggg iii kkk
ccc"
NG