Here's an implementation with expl3
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
% first of all we define the user level commands
\NewDocumentCommand{\inputcommandname}{ m }{ \malmedal_input_add:n { #1 } }
\NewDocumentCommand{\outputinorder}{ }{ \malmedal_output_direct: }
\NewDocumentCommand{\outputinreverseorder}{ }{ \malmedal_output_reverse: }
% allocate some variables:
% 1. a sequence for global storage of the inputs;
% 2. a temporary sequence when we need to reverse;
% 3. a counter (integer variable)
\seq_new:N \g_malmedal_input_seq
\seq_new:N \l_malmedal_temp_seq
\int_new:N \l_malmedal_count_int
% store globally an input in the sequence
\cs_new:Npn \malmedal_input_add:n #1
{
\seq_gput_right:Nn \g_malmedal_input_seq { #1 }
}
% how to output in direct order; we simply do a mapping function calling
% \malmedal_print:n after incrementing the counter
\cs_new_protected:Npn \malmedal_output_direct:
{
\int_zero:N \l_malmedal_count_int
\seq_map_inline:Nn \g_malmedal_input_seq
{
\int_incr:N \l_malmedal_count_int
\malmedal_print:n { ##1 }
}
}
% how to output in reverse order; first we store a copy of the main
% sequence in the temporary one, that we reverse; then we set the
% integer variable to the length of the sequence; finally we map the
% temporary sequence, decrementing the counter after having printed
% its contents
\cs_new_protected:Npn \malmedal_output_reverse:
{
\seq_set_eq:NN \l_malmedal_temp_seq \g_malmedal_input_seq
\seq_reverse:N \l_malmedal_temp_seq
\int_set:Nn \l_malmedal_count_int { \seq_length:N \l_malmedal_temp_seq }
\seq_map_inline:Nn \l_malmedal_temp_seq
{
\malmedal_print:n { ##1 }
\int_decr:N \l_malmedal_count_int
}
}
% the printing macro; change here for adapting to your wishes
\cs_new:Npn \malmedal_print:n #1
{
Input ~ \int_to_arabic:n { \l_malmedal_count_int } ~ is ~ `#1' \par
}
\ExplSyntaxOff
\begin{document}
\inputcommandname{first input}
\inputcommandname{second input}
\inputcommandname{third input}
Here is direct order:
\outputinorder
\bigskip
Here is reverse order:
\outputinreverseorder
\end{document}
Each \inputcommandname
adds globally its argument to a sequence which we can use for printing in direct order or in reverse order by reversing it.
The output is
Here is direct order:
Input 1 is ‘first input’
Input 2 is ‘second input’
Input 3 is ‘third input’
Here is reverse order:
Input 3 is ‘third input’
Input 2 is ‘second input’
Input 1 is ‘first input’
Due to the changes made to expl3
in Summer 2012, in the code above \seq_length
should be replaced by \seq_count
\cs_new_protected:Npn \malmedal_output_reverse:
{
\seq_set_eq:NN \l_malmedal_temp_seq \g_malmedal_input_seq
\seq_reverse:N \l_malmedal_temp_seq
\int_set:Nn \l_malmedal_count_int { \seq_count:N \l_malmedal_temp_seq }
\seq_map_inline:Nn \l_malmedal_temp_seq
{
\malmedal_print:n { ##1 }
\int_decr:N \l_malmedal_count_int
}
}