converting several comma-separated lists into a table, one column per list
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\magicFunction}{m}
{
\jason_magic:n { #1 }
}
\int_new:N \l__jason_magic_cols_int
\int_new:N \l__jason_magic_rows_int
\tl_new:N \l__jason_magic_table_tl
\cs_new_protected:Nn \jason_magic:n
{
\int_zero:N \l__jason_magic_cols_int
\int_zero:N \l__jason_magic_rows_int
\tl_map_inline:nn { #1 }
{
\int_incr:N \l__jason_magic_cols_int
\__jason_magic_makeclist:n { ##1 }
}
\__jason_magic_maketable:
% print the table
\begin{tabular}{ c *{\l__jason_magic_cols_int}{c} }
\l__jason_magic_table_tl
\end{tabular}
}
% syntactic sugar for avoiding long strings
\cs_new:Nn \__jason_magic_clist:n
{
l__jason_magic_ \int_eval:n { #1 } _clist
}
% store the parts in clists and get the number of rows
\cs_new_protected:Nn \__jason_magic_makeclist:n
{
\clist_clear_new:c { \__jason_magic_clist:n { \l__jason_magic_cols_int } }
\clist_set:cn { \__jason_magic_clist:n { \l__jason_magic_cols_int } } { #1 }
\int_set:Nn \l__jason_magic_rows_int
{
\int_max:nn
{ \l__jason_magic_rows_int }
{ \clist_count:c { \__jason_magic_clist:n { \l__jason_magic_cols_int } } }
}
}
\cs_new_protected:Nn \__jason_magic_maketable:
{
% make the first row
\tl_set:Nn \l__jason_magic_table_tl { Problem~number }
\int_step_inline:nn { \l__jason_magic_cols_int }
{
\tl_put_right:Nx \l__jason_magic_table_tl
{
& Form~\int_to_Alph:n { ##1 }
}
}
\tl_put_right:Nn \l__jason_magic_table_tl { \\ \hline }
% make the following rows
\int_step_inline:nn { \l__jason_magic_rows_int }
{
\tl_put_right:Nx \l__jason_magic_table_tl { ##1 }
\int_step_inline:nn { \l__jason_magic_cols_int }
{
\tl_put_right:Nx \l__jason_magic_table_tl
{
& \clist_item:cn { \__jason_magic_clist:n { ####1 } } { ##1 }
}
}
\tl_put_right:Nn \l__jason_magic_table_tl { \\ }
\int_if_odd:nF { ##1 } { \tl_put_right:Nn \l__jason_magic_table_tl { \hline } }
}
}
\ExplSyntaxOff
\begin{document}
\magicFunction{
{b, b, a, a, b, b, c}% Test Form A, form is first entry.
{a, b, c, c, e, a, a}% Test form B
{c, c, c, a, a, b, c}% Test form C
}
\end{document}
The idea is to allocate a clist for every column. Then the table is built by extracting the relevant item from each clist, by stepping the row index.