Re: Convert any list to a table?
Re: Convert any list to a table?
- Subject: Re: Convert any list to a table?
- From: George Mack <email@hidden>
- Date: Wed, 19 Sep 2001 09:52:28 -0400
Jason bourke asked:
--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.
--Below is my code that works for a 2 column table.
--Thanks
--Jason Bourque
Jason
Since I wrote a script earlier this summer that divides a text list
(in Quark) into tabbed tables, I drew on that to look at your script.
The problem is that when you need to pad the table with blanks (for
cells in the last column after you finish your data), the script
errors, and your try doesn't provide an out. The solution (at least
one solution) is to test to see if there actually is a value for the
cell and if not provide a pad. If you would rather not have a string
at the end, you might just put in the return at that point instead of
further down. Depends on what you need later from this pseudotable.
I logged each variable each time through. The expression
"item (((columnNth * rowsNeeded) - rowsNeeded) + rowsNth) of anylist"
threw an error when it tried to supply an item greater than anyListCount.
--- revised script; watch wraps
on createFalseTable(anyList, columnsNeeded)
set anyListCount to count of anyList
--log anyListCount
set rowsNeeded to (anyListCount div columnsNeeded) +
(anyListCount mod columnsNeeded)
--log rowsNeeded
set newList to {}
repeat with rowsNth from 1 to rowsNeeded
repeat with columnNth from 1 to columnsNeeded
try
if (((columnNth * rowsNeeded) - rowsNeeded) +
rowsNth) > anyListCount then
set end of newList to "" -- padding entry; I
assume it is to be a string, like sample
else
set end of newList to item (((columnNth *
rowsNeeded) - rowsNeeded) + rowsNth) of anyList
--> Can't get item 9 of {"aaa", "bbb", "ccc",
"ddd", "eee", "fff", "ggg", "hhh"}.
end if
if columnsNeeded = columnNth then
set end of newList to return
beep --not beeping with odd columns
else
set end of newList to tab
end if
-- on error errmsg
--log columnNth & tab & item ((columnNth *
rowsNeeded) - rowsNeeded + rowsNth) of anyList & return
end try
--log newList
end repeat
end repeat
items 1 thru -2 of newList as string
end createFalseTable
--- end script
--- test sequence ---
set anyList to {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg",
"hhh", "iii", "jjj", "kkk"}
-- a list with prime number of entries to avoid the "easy mod" situation
log my createFalseTable(anyList, 3)
--- end test
Hope this helps
George
--
GEORGE MACK DESIGN - Scituate, MA 02066 - (781) 544-3880
Design-Illustration-Desktop Publishing-Miscellaneous Magic