Re: Convert any list to a table?
Re: Convert any list to a table?
- Subject: Re: Convert any list to a table?
- From: "Bob.Kalbaugh" <email@hidden>
- Date: Thu, 20 Sep 2001 22:39:22 -0500
on 9/18/01 9:53 PM, Jason Bourque at email@hidden wrote:
>
Ok, before I pull out any hair.
I lost a handful just working on this :-)
I know I'm late to this, but please, read on.
>
--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.
>
Given your example:
set anyList to {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh"}
and your need of returning items -down- the columns:
>
but should return
>
"aaa ddd ggg
>
bbb eee hhh
>
ccc fff"
I found the puzzle to be quite perplexing because it creates a restriction
to the amount of columns! For example, what if you wanted a 5 column table?
aaa ccc eee ggg hhh
bbb ddd fff
what if 6 ?
aaa ccc eee fff ggg hhh
bbb ddd
See, it doesn't give you what you wanted. The MOST columns you can have
provided your example would be 4.
aaa ccc eee ggg
bbb ddd fff hhh
And the restriction will hold true for any number of items. Eventually the
amount of columns desired would overide the count of the items in rows, thus
not returning what you wanted.
Take Nigel's solution for example. It returned
--> "aaa ddd fff hhh jjj
bbb eee ggg iii kkk
ccc"
It's wonderful, but it's not what you asked for. It might be what you settle
for, but logically, "fff" should be under "ggg" and so on, given your
request. Throw 6 at it, or 7. See what I mean? It doesn't lend itself to an
alphabetical list that travels -down- columns.
Oh well. Just for the sake of contribution, I thought I would add my
solution. Its not as purty as the others but it does work, is as well
thought out as R.P.'s but on a MUCH lower level, and as long as you don't
mind the column restrictions, I think it gives you exactly what you're
looking for.
It looks longer than it really is (48 lines). That's because I've tried my
best to comment it, but throw some numbers at it and check the results. For
now I am really happy with it.
In the meantime I am seriously going to try and digest Mr. Philips'
solution. Amazing! I guess that's why he's a professor...and I'm not.
Thanks again for the puzzle.
_bob.kalbaugh
-- begin script ---------------------------------------------------------
-- part 1 (the set-up)
-- MIND THE WRAP!!
set anyList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
-- set this to however many columns you want
set cCount to 3
-- get an item count of the list
set iCount to (count items in anyList)
-- determine your rows this way
set rCount to round (iCount / cCount) rounding up
-- were going to sub-divide the list into smaller lists
-- so initialize a variable for a new row
-- this becomes evident later
set startRow to (rCount + 1)
-- part 2 (the work)
-- spark the handler
set myTable to makeTable(rCount, cCount, startRow, anyList)
-- delete this part only when testing the sub-lists below --
-- get rid of last return
set myTable to (text 1 thru -2 of myTable) as string
-- put the table on the clipboard
-- for pasting into (QXP?)
set the clipboard to myTable
-- end this part --
-- (the Handler)
on makeTable(rCount, cCount, startRow, theList)
-- because we're going to shuffle the list
-- let's create a temporary copy to work on
set tempList to theList
-- Create a holder for the sub-lists.
-- These sub-lists will hold the items of
-- the list in a natural order (ala Ric Philips) IE:
-- {{"a", "b", "c", "d"}, {"e", "f", "g", "h"}, {"etc"}}
-- we'll parse these sub-lists further down
-- to get the order you want. If you'll note, the
-- sub-lists actually contain the column data
set newList to {}
-- I could explain how this works
-- but I'm not going to : -)
repeat until tempList is {}
try
copy ((items 1 thru rCount of tempList) as list) to end of newList
on error -- it WILL error (thats ok)
copy ((items 1 thru -1 of tempList) as list) to end of newList
-- log "!!"
end try
try
set tempList to items startRow thru -1 of tempList
on error -- it WILL error (thats ok)
set tempList to {}
-- log "!!!"
end try
end repeat
-- to test the sublists --
-- newList
-- end makeTable -- ( watch out !)
-- clip the script here and call the resulting newList if
-- you want to see the sub-lists BUT!! IMPORTANT!!!
-- make sure you delete the part above that
-- deletes the last return and sets the clipboard
-- okay, now to parse the sub-lists
-- filter thru the sub-lists grabbing the first item of each
-- and temporarily put them in an empty list
-- coerce this new list to text adding tabs & returns
-- copying result to the text table
-- initialize the text table
set tableText to ""
-- start the filter
repeat rCount times
initialize the empty container (resets each loop)
set tableList to {}
repeat with i from 1 to count newList
try
copy item 1 of item i of newList to end of tableList
-- reset the sub-list for the repeat
-- it's another shuffle
set item i of newList to rest of item i of newList
on error -- it WILL eventually error (thats ok)
-- log "!!!!"
end try
end repeat
-- coerce to text ( this part sucks because
-- it has to keep rewriting the text table &
-- will be slow for l o n g lists) - [oh well]
set AppleScript's text item delimiters to tab
set tableText to tableText & (tableList as text)
-- note that this will add a return to the final row
-- which is not what you wanted, but we can ditch it later
set tableText to tableText & return
end repeat
-- reset delimiters
set AppleScript's text item delimiters to ""
return tableText
end makeTable
--
myTable -- with 3
result -->
a j s
b k t
c l u
d m v
e n w
f o x
g p y
h q z
i r