3

Suppose, I have a sequence of 2 plists:

(
{
      1 => 2,
      3 => 4
},
{
      5 => 6,
      hello => 8
}
)

I am traversing through the sequence and want to output only plists, containing the key "hello". I created this code:

\documentclass{article}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
%sequence definition
\clist_new:N\l_my_clist

%plist1 definition
\prop_new:N \prop_list_a
\prop_put:Nnn \prop_list_a {1}{2}
\prop_put:Nnn \prop_list_a {3}{4}

%plist2 definition
\prop_new:N \prop_list_b
\prop_put:Nnn \prop_list_b {5}{6}
\prop_put:Nnn \prop_list_b {hello}{8}

%putting plists to sequence
\clist_set:Nn \l_my_clist {\prop_list_a, \prop_list_b}

%output loop
\clist_map_inline:Nn \l_my_clist
{
  \prop_get:NnNTF #1 {hello} \var
  {
    \prop_map_inline:Nn #1
    {
      [##1] ~[##2]\\
    }
  }
}
\ExplSyntaxOff
\end{document}

It outputs this:

enter image description here

  1. Why did the comma appear?
  2. How to get rid of it?
  3. How to use \var in \prop_get:NnNTF? What was it designed for?
7

You have used \prop_get:NnNTF but have not given a false branch in your code, so you are getting odd effects. You seem to want something like

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

%plist1 definition
\prop_new:N \l_list_a_prop
\prop_put:Nnn \l_list_a_prop { 1 } { 2 }
\prop_put:Nnn \l_list_a_prop { 3 } { 4 }

%plist2 definition
\prop_new:N \l_list_b_prop
\prop_put:Nnn \l_list_b_prop { 5 } { 6 }
\prop_put:Nnn \l_list_b_prop { hello } { 8 }

%putting plists to sequence
\seq_new:N \l_my_seq
\seq_push:Nn \l_my_seq { \l_list_a_prop }
\seq_push:Nn \l_my_seq { \l_list_b_prop }
\seq_map_inline:Nn \l_my_seq
  {
    \prop_get:NnNT #1 { hello } \l_tmpa_tl
      {
        \prop_map_inline:Nn #1 { [ ##1 ] ~ [ ##2 ] \\ }
    }
  }
\ExplSyntaxOff
\end{document}

(I've tided up variable names and used a sequence rather than a comma list: the latter are really meant mainly for using with 'user level' input.)

The idea of \prop_get:NnN(TF) is that it recovers the value associated with the key and stores it in the token list. You can then use it:

\prop_put:Nnn \l_tmpa_prop { foo } { bar }
\prop_get:NnNTF \l_my_prop { foo } \l_tmpa_tl
  {
    % \l_tmpa_tl is set to 'bar' and can be used, for example:
    \tl_use:N \l_tmpa_tl
  }
  {
    % \l_tmpa_tl is not in a defined state
  }
  • You didn't use the false branch either in \prop_get:NnNT, so this was not the issue. It seems, that comma disappeared, because you used a sequence instead of clist. What was wrong with the clist? – user4035 Apr 16 '13 at 7:55
  • 1
    @user4035 Note the missing F in NnNT which eliminates the need for a false branch. – Scott H. Apr 16 '13 at 8:05
  • 1
    @user4035 I didn't use a false branch as I used \prop_get:NnNT rather than \prop_get:NnNTF. Comma lists are more limited than sequences: they can't hold empty values, 'odd' input (for example commas!), etc. Sequences can do all of these things, and we can gain some speed in some internals as a result (\seq_map_inline:Nn is faster than \clist_map_inline:Nn, for example). – Joseph Wright Apr 16 '13 at 8:13

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.