Re: Convert any list to a table?
Re: Convert any list to a table?
- Subject: Re: Convert any list to a table?
- From: has <email@hidden>
- Date: Wed, 19 Sep 2001 05:05:03 +0100
Jason Bourque wrote:
>
Ok, before I pull out any hair.
Ooh, fun. Part of it is that you're building a table with the content
running down the way, rather than across (which is more common, I think).
Despite this, you're still specifying width (note this has implications for
how the finished table appears). Also, you're feeding it a list of x items,
where the number of columns may not be a multiple of x. Plus you only want
to add tabs when they're followed by something.
Messing around with it, I eventually came up with a solution that looks
almost, though not quite, entirely unlike the original.:)
=================================================
on createFalseTable(anyList, columnsNeeded)
set newList to {}
set anyListCount to count of anyList
--calculate number of rows needed (number of complete rows, plus a
partial row if necessary)
set rowsNeeded to (anyListCount div columnsNeeded)
if (anyListCount mod columnsNeeded) > 0 then set rowsNeeded to
rowsNeeded + 1
log {rowsNeeded}
--add every item in anyList to newList, getting them in the appropriate
order
repeat with itemCount from 0 to (rowsNeeded * columnsNeeded - 1)
--set columnNumber to (itemCount mod rowsNeeded)
--find the position of the item in anyList to be added next (the
yucky maths bit)
set getPositionInAnyList to ((itemCount mod columnsNeeded) *
rowsNeeded + 1) + (itemCount div columnsNeeded)
--don't try getting item if getPositionInAnyList > number of items in
anyList
if getPositionInAnyList is not greater than anyListCount then
--add item, preceded by either tab or return as required
if (itemCount mod columnsNeeded) = 0 then
set newList to newList & return & item getPositionInAnyList of
anyList
else
set newList to newList & tab & item getPositionInAnyList of anyList
end if
(*else
--add padding to fill out all columns
set newList to newList & tab & "[padding]"*)
end if
end repeat
return (items 2 thru -1 of newList) as string
end createFalseTable
set anyList to {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh",
"iii", "jjj", "kkk"}
my createFalseTable(anyList, 4)
=================================================
Now, as I mentioned above, the rather topsy-turvy approach to tables used
here has an interesting implication in practice: columnsNeeded works only
as a maximum value, not as an absolute number of columns. This means that
columns will fill up only as needed. For example, try "my
createFalseTable(anyList, 5)" with the above anyList: it'll generate only
four columns before it runs out of content.
If you want to fill out empty columns with padding, enable the commented
out lines:
(*else
--add padding to fill out all columns
set newList to newList & tab & "[padding]"*)
HTH.
has