Following up this answer, how to robustly use the values stored in \getRow as coefficients of a polynomial to be printed?
Also, I need to have an argument of the symbol to be used in the polynomial. (it is v here in this MWE)
Please, note that the zero, in a cell, is used as a coefficient of a polynomial term, while the blank cell is not used in the polynomial construction (i.e. case Third here)
\begin{filecontents*}[overwrite]{mycoeffs.csv}
First , 4 , 0 , 2 , 5
Second , 0 , 0 , 7 , 8
Third , 5 , 0 , 6 ,
\end{filecontents*}
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
% Step 1: reading the file
\ior_new:N \l__diaa_csv_ior
\bool_new:N \l__diaa_csv_str_bool
\seq_new:N \l__diaa_csv_tmp_seq
% str mode (bool/star), key column, label, value columns, file
\NewDocumentCommand \ReadCSV { s O{1} m O{} m }
{
\IfBooleanTF {#1}
{ \bool_set_true:N \l__diaa_csv_str_bool }
{ \bool_set_false:N \l__diaa_csv_str_bool }
\diaa_csv_read:nnnn {#3} {#2} {#4} {#5}
}
% label, key column, value columns, file
\cs_new_protected:Npn \diaa_csv_read:nnnn #1 #2 #3 #4
{
\tl_if_blank:nTF {#3} % Detect number of columns and use 2 to last
{
\ior_open:NnTF \l__diaa_csv_ior {#4}
{
\bool_if:NTF \l__diaa_csv_str_bool
{ \ior_str_get:NN }
{ \ior_get:NN }
\l__diaa_csv_ior \l_tmpa_tl
\ior_close:N \l__diaa_csv_ior
\seq_set_split:NnV \l_tmpa_seq { , } \l_tmpa_tl
\seq_clear:N \l__diaa_csv_tmp_seq
\int_step_inline:nnn { 2 } { \seq_count:N \l_tmpa_seq }
{ \seq_put_right:Nn \l__diaa_csv_tmp_seq {##1} }
}
{ \msg_error:nnn { diaa } { file-not-found } {#4} }
}
{ \seq_set_split:Nnn \l__diaa_csv_tmp_seq { , } {#3} } % explicit columns
\ior_open:NnTF \l__diaa_csv_ior {#4}
{
\prop_new:c { g__diaa_csv_#1_prop }
\__diaa_csv_read:nn {#1} {#2}
\ior_close:N \l__diaa_csv_ior
}
{ \msg_error:nnn { diaa } { file-not-found } {#4} }
}
\msg_new:nnn { diaa } { file-not-found }
{ File~`#1'~not~found. }
\cs_generate_variant:Nn \prop_put:Nnn { cxV }
% label, key column
\cs_new_protected:Npn __diaa_csv_read:nn #1 #2
{
\bool_if:NTF \l__diaa_csv_str_bool
{ \ior_str_map_inline:Nn }
{ \ior_map_inline:Nn }
\l__diaa_csv_ior
{
\seq_set_split:Nnn \l_tmpa_seq { , } {##1} % split one CSV row
\tl_clear:N \l_tmpa_tl
\seq_map_inline:Nn \l__diaa_csv_tmp_seq
{
\tl_put_right:Nx \l_tmpa_tl { { \seq_item:Nn \l_tmpa_seq {####1} } }
}
\prop_put:cxV { g__diaa_csv_#1_prop }
{ \seq_item:Nn \l_tmpa_seq {#2} }
\l_tmpa_tl
}
}
% Step 2: getting the values
% star → global assignment, macro or tl var, value column, key, label
\NewDocumentCommand \getValue { s m O{1} m m }
{
\IfBooleanTF {#1} { \tl_gset:Nx } { \tl_set:Nx }
#2 { \diaa_csv_item:nnn {#4} {#3} {#5} }
}
% key, value column, label
\NewExpandableDocumentCommand \CSVItem { m O{1} m }
{ \diaa_csv_item:nnn {#1} {#2} {#3} }
\cs_generate_variant:Nn \tl_item:nn { f }
% key, value column, label
\cs_new:Npn \diaa_csv_item:nnn #1 #2 #3
{
\tl_item:fn { \prop_item:cn { g__diaa_csv_#3_prop } {#1} } {#2}
}
% star → global assignment, macro, key, label
\NewDocumentCommand \getRow { s m m m }
{
\prop_get:cnN { g__diaa_csv_#4_prop } {#3} \l_tmpa_tl
\IfBooleanTF {#1} { \cs_gset_nopar:Npx } { \cs_set_nopar:Npx } #2 [ ##1 ]
{
\exp_not:N \str_if_eq:nnTF {##1} { non-empty }
{
\exp_not:N __diaa_nb_nonempty_items_in_row:nw { 0 }
\exp_not:V \l_tmpa_tl
\exp_not:n { \q_recursion_tail \q_recursion_stop }
}
{ \exp_not:N \tl_item:nn { \exp_not:V \l_tmpa_tl } {##1} }
}
}
\cs_new:Npn __diaa_nb_nonempty_items_in_row:nw #1#2
{
\quark_if_recursion_tail_stop_do:nn {#2} { \int_eval:n {#1} }
\tl_if_empty:nTF {#2}
{ __diaa_nb_nonempty_items_in_row:nw {#1} }
{ __diaa_nb_nonempty_items_in_row:nw { #1 + 1 } }
}
\ExplSyntaxOff
\begin{document}
\ReadCSV{mydata}{mycoeffs.csv}
\getRow\First{First}{mydata}
I need to use \verb|\First| to print $4 \times v^3 + 2 \times v + 5$
\getRow\Second{Second}{mydata}
I need to use \verb|\Second| to print $7 \times v + 8$
\getRow\Third{Third}{mydata}
I need to use \verb|\Third| to print $5 \times v^2 + 6$
\end{document}


\ReadCSV{mydata}{mycoeffs.csv} ... \getPolyFromRow{\First}{First}{mydata} ... $\First$. Do you want the variable name to be an argument of\getPolyFromRowor of\First? And should it be a mandatory argument in braces, or an optional argument in square brackets with some default? – frougon Feb 22 '22 at 18:28\getPolyFromRowwith any default one. Please, note the variable may be something likeV_xnot just a single character. – Diaa Feb 22 '22 at 18:37