我如何从序列中删除最后一个项目?我阅读了 expl3 文档,我拥有的是\seq_gpop_left/right
,但我不想在某些 tlist 中复制已删除的项目。我还想知道,例如,如何在 expl3 序列中按索引删除项目。感谢大家的回答。
答案1
要删除序列的最后一项,请使用\seq_(g)pop_right:NN
临时标记列表。没有expl3
“直接删除”项并将其丢弃的功能,因为您通常需要弹出的项。当您不需要它时,只需忽略输出标记列表的内容即可。
没有函数可以删除索引项,但构建自己的函数并不难。下面是一个用于循环遍历的\kozlovskiy_(g)remove_seq_item:Nn <seq var> { <index> }
函数,它从中删除项目编号。\seq_map_indexed_inline:Nn
<seq var>
<index>
\documentclass{article}
\ExplSyntaxOn
\seq_new:N \l__kozlovskiy_remove_tmp_seq
\cs_new_protected:Npn \kozlovskiy_remove_seq_item:Nn
{ \__kozlovskiy_remove_seq_item:NNn \seq_set_eq:NN }
\cs_new_protected:Npn \kozlovskiy_gremove_seq_item:Nn
{ \__kozlovskiy_remove_seq_item:NNn \seq_gset_eq:NN }
\cs_new_protected:Npn \__kozlovskiy_remove_seq_item:NNn #1 #2 #3
{
\seq_map_indexed_inline:Nn #2
{
\int_compare:nNnF {#3} = {##1}
{ \seq_put_right:Nn \l__kozlovskiy_remove_tmp_seq {##2} }
}
#1 #2 \l__kozlovskiy_remove_tmp_seq
}
\ExplSyntaxOff
\begin{document}
\ExplSyntaxOn
\seq_new:N \g__kozlovskiy_vars_seq
\tl_new:N \g__kozlovskiy_tmp_tl
% Fill the sequence with some items
\seq_gset_from_clist:Nn \g__kozlovskiy_vars_seq
{ some, meaningless, items, to, populate, the, sequence, variable }
% To remove the last item use \seq_gpop_right:NN and just discard the token list
\seq_show:N \g__kozlovskiy_vars_seq
\seq_gpop_right:NN \g__kozlovskiy_vars_seq \g__kozlovskiy_tmp_tl
% To remove the item in <index> use this function:
\seq_show:N \g__kozlovskiy_vars_seq
\kozlovskiy_gremove_seq_item:Nn \g__kozlovskiy_vars_seq { 2 }
\seq_show:N \g__kozlovskiy_vars_seq
\end{document}
终端显示(注意,首先{variable}
删除,然后{meaningless}
):
The sequence \g__kozlovskiy_vars_seq contains the items (without outer
braces):
> {some}
> {meaningless}
> {items}
> {to}
> {populate}
> {the}
> {sequence}
> {variable}.
<recently read> }
l.32 \seq_show:N \g__kozlovskiy_vars_seq
?
The sequence \g__kozlovskiy_vars_seq contains the items (without outer
braces):
> {some}
> {meaningless}
> {items}
> {to}
> {populate}
> {the}
> {sequence}.
<recently read> }
l.36 \seq_show:N \g__kozlovskiy_vars_seq
?
The sequence \g__kozlovskiy_vars_seq contains the items (without outer
braces):
> {some}
> {items}
> {to}
> {populate}
> {the}
> {sequence}.
<recently read> }
l.38 \seq_show:N \g__kozlovskiy_vars_seq
?