使用 \seq_item 時阻止整數擴展

使用 \seq_item 時阻止整數擴展

我有兩個序列:\l_words_seq包含:{ dans, [DP, la, maison, ]foc } 和 \l_glosses_seq包含:{ in, the, house }。我想建立另一個序列\l_final_seq ,在對應的位置\l_glosses_seq插入\l_words_seq符合正規表示式的項目。^(\[(DP|NP)|\]\w* )結果應該是 { in, [DP, the, house, ]foc }

\int_new:N \l_counter_glosses_int

\seq_const_from_clist:Nn \l_words_seq {dans, [DP, la, maison, ]foc}
\seq_const_from_clist:Nn \l_glosses_seq {in, the, house}

\seq_new:N \l_final_seq

\int_zero:N \l_counter_glosses_int

\seq_map_inline:Nn \l_words_seq  {
    \regex_match:nnTF {^(\[(DP|NP)|\]\w* )} { #1 } 
    { \seq_put_right:Nn \l_final_seq {#1} }             
    {
        \int_incr:N  \l_counter_glosses_int 
        \seq_put_right:No \l_final_seq { \seq_item:Nn  \l_glosses_seq  {\int_use:N \l_counter_glosses_int}} 
    }
}

\seq_map_inline:Nn \l_final_seq  {  #1~ }

但我的結果是

house [DP house house ]foc

我該如何解決這個問題?我嘗試過\seq_put_right:No,但它僅在我附加整數而不是與該整數對應的項目時才有效。

答案1

解決方案是使用\seq_put_right:Ne(或Nx) 來代替,在將插入的項目添加到序列之前將其完全展開。

o類型執行單一擴展,這在您嘗試透過本身儲存在變數中的索引從序列中取得項目時是不夠的。

使用etype (或xtype)將擴展該項目,包括整數值,這將為您提供預期的結果。

\documentclass{article}

\begin{document}
\ExplSyntaxOn
\int_new:N \l_mymodule_glosses_int

\seq_const_from_clist:Nn \l_mymodule_words_seq {dans, [DP, la, maison, ]foc}
\seq_const_from_clist:Nn \l_mymodule_glosses_seq {in, the, house}

\seq_new:N \l_mymodule_final_seq

\int_zero:N \l_mymodule_glosses_int

\seq_map_inline:Nn \l_mymodule_words_seq  {
    \regex_match:nnTF {^(\[(DP|NP)|\]\w*)} { #1 } 
    { \seq_put_right:Nn \l_mymodule_final_seq {#1} }             
    {
        \int_incr:N  \l_mymodule_glosses_int 
        \seq_put_right:Ne \l_mymodule_final_seq { \seq_item:Nn  \l_mymodule_glosses_seq  {\l_mymodule_glosses_int}}
    }
}

\seq_use:Nn \l_mymodule_final_seq { ~ }
\ExplSyntaxOff
\end{document}

我已經替換了最後一部分,\seq_use:Nn而不是使用內聯映射。


或者,您可以使用\seq_pop:NN將值從序列彈出到標記清單。然後,o類型擴展(或V,優於o)將可以很好地擴展標記列表變數。

\seq_pop:NN \l_mymodule_glosses_seq \l_tmpa_tl
\seq_put_right:NV \l_mymodule_final_seq \l_tmpa_tl

相關內容