2

How to count the number of lines in an array \def\datalist{16.03.2022/2,
28.06.2022/3, }

4
  • 1
    Can you clarify what you consider a line in this array? Commented Jun 20 at 11:23
  • -samcarter_is_at_topanswers.xyz 16.03.2022/2,
    – Juli
    Commented Jun 20 at 11:30
  • 1
    yes, but does the empty line at the end count too? Commented Jun 20 at 11:31
  • line - up to comma, two elements
    – Juli
    Commented Jun 20 at 11:38

3 Answers 3

6

If you define a line as "up to comma", you could count the number of commas in your macro:

\documentclass{article}

\usepackage{stringstrings}

\def\datalist{16.03.2022/2,
28.06.2022/3, }

\begin{document}

\findchars{\datalist}{,}

\end{document}
3
  • samcarter_is_at_topanswers.xyz - thank you, it worked
    – Juli
    Commented Jun 20 at 11:50
  • @Juli You're welcome! Commented Jun 20 at 11:51
  • @Juli Please accept the answer if it suits with your requirement by clicking the tick mark
    – MadyYuvi
    Commented Jun 20 at 11:55
4

If your “arrays” are actually comma separated lists of items, you can do it very easily:

\documentclass{article}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\countitems}{m}
 {
  \clist_count:N #1
 }
\ExplSyntaxOff

\newcommand{\datalist}{16.03.2022/2, 28.06.2022/3, }

\begin{document}

\countitems{\datalist}

\end{document}

This would print 3. If you want to avoid empty items, you can't do it expandably.

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\countitems}{m}
 {
  \clist_set:No \l_tmpa_clist { #1 }
  \clist_count:N \l_tmpa_clist
 }
\ExplSyntaxOff

\newcommand{\datalist}{16.03.2022/2, 28.06.2022/3, }

\begin{document}

\countitems{\datalist}

\end{document}

However, I suggest a different approach.

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\newarray}{mm}
 {
  \clist_new:c { l_juli_array_#1_clist }
  \clist_put_right:cn { l_juli_array_#1_clist } { #2 }
 }
\NewDocumentCommand{\appendtoarray}{mm}
 {
  \clist_put_right:cn { l_juli_array_#1_clist } { #2 }
 }
\NewDocumentCommand{\prependtoarray}{mm}
 {
  \clist_put_left:cn { l_juli_array_#1_clist } { #2 }
 }
\NewExpandableDocumentCommand{\countarray}{m}
 {
  \clist_count:c { l_juli_array_#1_clist }
 }
\ExplSyntaxOff

\newarray{datalist}{16.03.2022/2, 28.06.2022/3, }

\begin{document}

\countarray{datalist}

\appendtoarray{datalist}{xyz}

\countarray{datalist}

\end{document}

This will print 2 and 3.

Why is this better? Because you can easily process your array:

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\newarray}{mm}
 {
  \clist_new:c { l_juli_array_#1_clist }
  \clist_put_right:cn { l_juli_array_#1_clist } { #2 }
 }
\NewDocumentCommand{\appendtoarray}{mm}
 {
  \clist_put_right:cn { l_juli_array_#1_clist } { #2 }
 }
\NewDocumentCommand{\prependtoarray}{mm}
 {
  \clist_put_left:cn { l_juli_array_#1_clist } { #2 }
 }
\NewExpandableDocumentCommand{\countarray}{m}
 {
  \clist_count:c { l_juli_array_#1_clist }
 }
\NewDocumentCommand{\usearray}{O{,~}mo}
 {
  \IfNoValueTF{#3}
   {
    \clist_use:cn { l_juli_array_#2_clist } { #1 }
   }
   {
    \clist_map_inline:cn { l_juli_array_#2_clist } { #3 }
   }
 }

\ExplSyntaxOff

\newarray{datalist}{16.03.2022/2, 28.06.2022/3, }

\begin{document}

\countarray{datalist}

\appendtoarray{datalist}{xyz}

\countarray{datalist}

\usearray{datalist}

\usearray[ -- ]{datalist}

\begin{itemize}
\usearray{datalist}[\item #1]
\end{itemize}

\end{document}

enter image description here

If you specify the last optional argument to \usearray, this is a template to which every item in the array is passed; the current item is denoted #1.

Otherwise, the array will be simply printed with the separator specified in the first optional argument (default “comma and space”).

3

The listofitmes package can parse, based on a specified separator. In this case, I use (the default) comma as the separator. The command \ignoreemptyitems can be specified if one wishes to ignore empty list items.

While not shown, individual array elements can be expandably recalled, for example, via \myarray[1], \myarray[2], etc.

\documentclass{article}
\usepackage{listofitems}
\def\datalist{16.03.2022/2,
28.06.2022/3, }
%\setsepchar{,} % comma is the default separator
\begin{document}
% star version of \readlist clips leading/trailing spaces
\readlist*\myarray{\datalist}
\myarraylen

\ignoreemptyitems %will not count blank array elements
\readlist*\myarray{\datalist}
\myarraylen
\end{document}

enter image description here

You must log in to answer this question.

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