5

Is there a function/command/macro that gives the length of an expl3 sequence? I know that you can do something like the code below but I thought that there would be a more direct way of doing this since you can pop items from either end of the sequence and, internally, the sequence probably does know its own length.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \l_tmp_seq
\seq_set_from_clist:Nn \l_tmp_seq {1,2,3,4,5,6}

\int_new:N \l_my_int
\int_set:Nn \l_my_int {0}

\seq_map_inline:Nn \l_tmp_seq {% loop through the sequence and count its length
   \int_incr:N \l_my_int
}
\NewDocumentCommand\SequenceLength{}{
   The~sequence~has~length~\int_use:N \l_my_int
}
\ExplSyntaxOff

\begin{document}

    \SequenceLength

\end{document}

As you might expect, this prints:

enter image description here

  • 2
    \seq_count:N?! – TeXnician Oct 12 '17 at 8:46
7

In expl3 this is always the count of a variable, so you want \seq_count:N (cf. \tl_count:N, \prop_count:N, etc.).

  • Thanks! Not sure how I missed this in the manual. – Andrew Oct 12 '17 at 8:53
  • @Andrew Perhaps you were looking for length: we used to call it that, but something like a prop doesn't really have length, hence the name change. – Joseph Wright Oct 12 '17 at 8:54
  • You're being too kind: I read through the entire section and just missed it:) – Andrew Oct 12 '17 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.