2

How I can remove the last item from a sequence? I read the expl3 documentation, what I have is \seq_gpop_left/right, but I don't want to copy the deleted item in some tlist. Also I want to know, for example, how to remove items by index in expl3 sequences. Thanks everybody for answers.

1 Answer 1

4

To remove the last item of a sequence, use \seq_(g)pop_right:NN with a temporary token list. There is no function in expl3 to “just remove” an item throwing it away because more often than not you need the popped item. When you don't need it, just ignore the contents of the output token list.

There is no function to remove an indexed item, but it's not hard to build your own. Here's a \kozlovskiy_(g)remove_seq_item:Nn <seq var> { <index> } that uses \seq_map_indexed_inline:Nn to loop over the <seq var>, and removes item number <index> from it.

\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}

The terminal shows (note that first {variable} is removed, then {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
                                            
?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .