
我按照此處的答案將字串列表(命令字串)存儲在數組中:https://tex.stackexchange.com/a/215571/61517。現在我想隨機存取它們,因此我lcq
向其中添加了一個隨機計數器(透過使用):
\storedata{general}{{a}{b}{c}}
\reinitrand[counter=GenCounter, last=3]
\getdata[\value{GenCounter}]{general}
我現在的問題是:我想general
透過新增更多指令來擴充 -array,但隨後我還必須修改-functionlast
的 -value\reinitrand[]
才能存取所有元素。是否可以向\storedata
-macro 添加一個計數器,以便查看數組的當前長度,並相應地設置 reinit-macro?
我的最終目標是隨機存取數組中的所有元素,無論數組中元素的數量如何,而不會出現越界異常導致任何結果的可能問題。此外,我不想last
每次更改數組中的命令數量時都更改隨機初始化的值,它應該自行更新。
擴充 2:我的程式碼如下所示(取自上述答案):
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\storedata}{mm}
{
\bcp_store_data:nn { #1 } { #2 }
}
\NewDocumentCommand{\appenddata}{mm}
{
\bcp_append_data:nn { #1 } { #2 }
}
\DeclareExpandableDocumentCommand{\getdata}{O{1}m}
{
\bcp_get_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_store_data:nn #1 #2
{
% create the sequence if it doesn't exist or clear it if it exists
\seq_if_exist:cTF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
{ \seq_clear:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_append_data:nn #1 #2
{
% create the sequence if it doesn't exist, do nothing if it exists
\seq_if_exist:cF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \__bcp_append_data:nn #1 #2
{
% append items one at a time
\tl_map_inline:nn { #2 }
{
\seq_put_right:cn { l_bcp_data_#1_seq } { ##1 }
}
}
\cs_new:Npn \bcp_get_data:nn #1 #2
{
% retrieve the requested item
\seq_item:cn { l_bcp_data_#2_seq } { #1 }
}
\ExplSyntaxOff
\begin{document}
\storedata{mydata}{{one}{two}}
\appenddata{mydata}{{three}{four}}
\getdata[1]{mydata}
\getdata[2]{mydata}
\getdata[3]{mydata}
\getdata[4]{mydata}
\printlength{mydata}% How can I define this function?
\end{document}
我如何得到 的長度mydata
,即 的結果\printlength
?
答案1
在包裝器命令中使用\seq_count:N
將提供序列中的元素數量。
這裡必須使用變體,\seq_count:c
因為序列名稱是產生的並且之前在\l_...seq
類似的命令名稱中是未知的。
可以看出,\printlength
宏是可擴展的。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\storedata}{mm}
{
\bcp_store_data:nn { #1 } { #2 }
}
\NewDocumentCommand{\appenddata}{mm}
{
\bcp_append_data:nn { #1 } { #2 }
}
\DeclareExpandableDocumentCommand{\getdata}{O{1}m}
{
\bcp_get_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_store_data:nn #1 #2
{
% create the sequence if it doesn't exist or clear it if it exists
\seq_if_exist:cTF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
{ \seq_clear:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_append_data:nn #1 #2
{
% create the sequence if it doesn't exist, do nothing if it exists
\seq_if_exist:cF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \__bcp_append_data:nn #1 #2
{
% append items one at a time
\tl_map_inline:nn { #2 }
{
\seq_put_right:cn { l_bcp_data_#1_seq } { ##1 }
}
}
\cs_new:Npn \bcp_get_data:nn #1 #2
{
% retrieve the requested item
\seq_item:cn { l_bcp_data_#2_seq } { #1 }
}
\newcommand{\printlength}[1]{%
\seq_count:c { l_bcp_data_#1_seq }
}
\ExplSyntaxOff
\begin{document}
\storedata{mydata}{{one}{two}}
\appenddata{mydata}{{three}{four}}
\getdata[1]{mydata}
\getdata[2]{mydata}
\getdata[3]{mydata}
\getdata[4]{mydata}
\ifnum\printlength{mydata} > 1\relax
Hooray% How can I define this function?
\else
Nope!
\fi
\end{document}