3

For some reason, using a variable as index of a list-item does not work for retrieving that item. But using a number instead of a variable works. Why?

\documentclass{standalone}
\usepackage{expl3}
\ExplSyntaxOn
\begin{document}

  \clist_new:N\myList
  \clist_set:Nn\myList{1,south}

  \int_new:N\myCounter
  \int_set:Nn\myCounter{1}
  \int_do_until:nNnn{\myCounter}={\clist_count:N\myList+1}
    { \regex_set:Nn\myExp{[0-9]+}
      % following line doesn't work
      %\regex_match:NnT\myExp{\clist_item:Nn\myList{\int_use:N\myCounter}}
      % but as soon as we change \int_use:N\myCounter to 1, it works
      \regex_match:NnT\myExp{\clist_item:Nn\myList{1}}
        { (is a number) % output
        }
      \int_incr:N\myCounter
    }

\end{document}
4

You are using a regex against the literal text \clist_item:Nn\myList{\int_use:N\myCounter}, not the result of executing it. In the case where you use an explicit 1, that works, but not when you have no 1 present.

Fixing that by expansion, and also using standard variable names plus a step loop (seems easier here), I'd have

\documentclass{standalone}
\usepackage{expl3}

\begin{document}
\ExplSyntaxOn
  \clist_set:Nn \l_tmpa_clist { 1 , south }
  \regex_set:Nn \l_tmpa_regex { [0-9]+ }
  \int_step_inline:nn { \clist_count:N \l_tmpa_clist }
    {
      \exp_args:NNf \regex_match:NnT \l_tmpa_regex
        { \clist_item:Nn \l_tmpa_clist {#1} }
        { (is ~ a ~ number ) }
    }
\ExplSyntaxOff

\end{document}
|improve this answer|||||
  • BTW, there's no benefit in pre-compiling the regex if you do it in every step of the loop ... – Joseph Wright May 16 '19 at 20:19
  • Isn't \clist_map_inline:Nn easier? – egreg May 16 '19 at 21:03
  • 1
    @egreg My guess was that the OP wants the position number too ... – Joseph Wright May 16 '19 at 21:04
5

The argument type n in \regex_match:NnT states that the argument is passed “literally”, with no processing, but you need the item, not the instructions needed to retrieve it. You could define a variant

\prg_generate_conditional_variant:Nnn \regex_match:nn { ne } { T,F,TF }

and use \regex_match:NeT, but it's actually much easier with \clist_map_inline:Nn that will access the clist's items one at a time:

\documentclass{article}
\usepackage{expl3}

\begin{document}

\ExplSyntaxOn

\clist_new:N \l_bp_mylist_clist
\clist_set:Nn \l_bp_mylist_clist {1,south}

\clist_map_inline:Nn \l_bp_mylist_clist
 {
  \regex_match:nnTF { [0-9]+ } { #1 }
   { #1~is~a~number \par }
   { #1~is~not~a~number \par }
 }

\ExplSyntaxOff

\end{document}

enter image description here

If you need to use the item number,

\documentclass{article}
\usepackage{expl3}

\begin{document}

\ExplSyntaxOn

\clist_new:N \l_bp_mylist_clist
\clist_set:Nn \l_bp_mylist_clist {1,south}

\prg_generate_conditional_variant:Nnn \regex_match:nn { ne } { T,F,TF }

\int_step_inline:nn { \clist_count:N \l_bp_mylist_clist }
 {
  \regex_match:neTF { [0-9]+ } { \clist_item:Nn \l_bp_mylist_clist { #1 } }
   { Item~#1~`\clist_item:Nn \l_bp_mylist_clist { #1 }'~is~a~number \par }
   { Item~#1~`\clist_item:Nn \l_bp_mylist_clist { #1 }'~is~not~a~number \par }
 }

\ExplSyntaxOff

\end{document}

enter image description here

|improve this answer|||||

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.