0

Well, I tried to understand your code or your code and I also consulted the tutorial indicated by cfr, and I admit that I don't understand anything and that it will take me a little time to master this language. Since you tell me that you have to ask the questions one by one, I would like to know how to retrieve the values of a line, for example the one obtained randomly, in nine respective variables varone, vartwo etc that I can then simply exploit in different calculations of the type (varone + vartwo) mod n or any other operation without having to continue in the same language? Because there, I have searched and I am unable to recover these values.

New contributor
Serge75013 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
7
  • 1
    Which code are you talking about? Who is "you"? Is this a follow-up to something? Commented 15 hours ago
  • I added a link to my answer, because I believe 'your' refers to mine. however, it would make sense if it referred to Explorer's instead (tex.stackexchange.com/a/755157), since that's the answer you accepted? you can edit the post to change the link. Commented 15 hours ago
  • @samcarter_is_at_topanswers.xyz ^^ I think the reference is to me, but I'm not certain. in terms of conversation, I seem to be intended, but in terms of ticks, I'd guess Explorer. (but poss. me because Explorer's is based on mine?) Commented 15 hours ago
  • 1
    @cfr Ah, thanks for the link. Make a bit more sense with the link. Commented 15 hours ago
  • 2
    please always make your posts self contained, should not not start a new question post as if carrying on a conversation with an unknown person started at an unknown location it makes the question impossible to understand for future readers. Commented 14 hours ago

1 Answer 1

1

Something like this?

  • \SetLine[<number>] sets a random or specified line;
  • \Element*[<i>]{<j>} retrieves the jth element of the current, ith or a random line.

retrieving data points in various ways

\documentclass{article}

% Built-in test data (9x9)
\ExplSyntaxOn
\clist_new:N \l__serge_mydata_clist
\seq_new:N \l__serge_lines_seq
\int_new:N \l__serge_lines_int
\int_new:N \l__serge_rand_int
\seq_new:N \l__serge_currline_seq
\seq_set_from_clist:Nn \l__serge_lines_seq
{
  {1,2,3,4,5,6,7,8,9},
  {10,11,12,13,14,15,16,17,18},
  {19,20,21,22,23,24,25,26,27},
  {28,29,30,31,32,33,34,35,36},
  {37,38,39,40,41,42,43,44,45},
  {46,47,48,49,50,51,52,53,54},
  {55,56,57,58,59,60,61,62,63},
  {64,65,66,67,68,69,70,71,72},
  {73,74,75,76,77,78,79,80,81x} % make sure non-int works
}
\seq_map_indexed_inline:Nn \l__serge_lines_seq
{
  \seq_new:c {l__serge_line_#1_seq}
  \seq_set_from_clist:cn {l__serge_line_#1_seq} {#2}
}
\int_set:Nn \l__serge_lines_int {\seq_count:N \l__serge_lines_seq}
\cs_new_protected:Npn \__serge_showlines:nn #1#2
{
  \int_set:Nn \l_tmpa_int {#1}
  \int_do_until:nn {\l_tmpa_int > #2} {
    \noindent Ligne~\int_to_arabic:n {\l_tmpa_int}: ~
    \seq_use:cn {l__serge_line_\int_to_arabic:n {\l_tmpa_int}_seq} {~}
    \par
    \int_incr:N \l_tmpa_int
  }
}
\cs_new_protected:Npn \__serge_findlinewith:n #1
{
  \int_step_inline:nn {\l__serge_lines_int}
  {
    \seq_if_in:cnT {l__serge_line_##1_seq} {#1}
    {
      \noindent Line~##1~contains~#1.\par
    }
  }
}
\cs_new_protected:Npn \__serge_set_currline:n #1
{
  \seq_set_eq:Nc \l__serge_currline_seq {l__serge_line_#1_seq}
}

\NewDocumentCommand \showlines {mm}
{
  \__serge_showlines:nn {#1} {#2}
}
\NewDocumentCommand \findlinewithx {m}
{
  \__serge_findlinewith:n {#1}
}
\NewDocumentCommand \SetLine {o} 
{
  \tl_if_novalue:nTF {#1}
  {
    \exp_args:Ne \__serge_set_currline:n {\int_rand:n {\seq_count:N \l__serge_lines_seq}}
  } {
    \__serge_set_currline:n {#1}
  }
}
\NewDocumentCommand \Element {som}
{
  \tl_if_novalue:nTF {#2}
  {
    \bool_if:NTF #1 
    {
      \seq_item:cn {l__serge_line_ \int_rand:n {\seq_count:N \l__serge_lines_seq} _seq} {#3}
    } {
      \seq_item:Nn \l__serge_currline_seq {#3}
    }
  } {
    \seq_item:cn {l__serge_line_#2_seq} {#3}
  }
}

\ExplSyntaxOff

\begin{document}
\thispagestyle{empty}
\section*{Displaying Lines 1 to 9}
\showlines{1}{9}


\section*{Line with specific number}
\findlinewithx{5} 

\SetLine[4] % set current line to 4th line
\Element{3} % 3rd element of current (4th) line

\Element[2]{5} % 5th element of 2nd line

\Element*{9} % random line, 9th element

\Element*{9} % random line, 9th element

\SetLine % pick random line

% print elements 1-9 of the currently line (i.e. the one just picked)
\Element{1} 
\Element{2}
\Element{3}
\Element{4}
\Element{5}
\Element{6}
\Element{7}
\Element{8}
\Element{9}

\end{document}

Edit

For calculations, you need expandable macros. Note also that integer calculations will often overflow. If you are unlucky, the code below gives an error. (It depends on which line is randomly selected.) Use floating point where this is a problem.

\documentclass[a4paper]{article}
\usepackage{geometry}
% ateb: https://tex.stackexchange.com/a/755177/
\ExplSyntaxOn
\clist_new:N \l__serge_mydata_clist
\cs_new_nopar:Npn \__serge_tmp_calc:nn #1#2 {}
\int_new:N \l__serge_lines_int
\int_new:N \l__serge_rand_int
\seq_new:N \l__serge_lines_seq
\seq_new:N \l__serge_currline_seq

\cs_new_protected:Npn \__serge_process_lines:n #1
{
  \seq_set_from_clist:Nn \l__serge_lines_seq {#1}
  \seq_map_indexed_inline:Nn \l__serge_lines_seq
  {
    \seq_new:c {l__serge_line_##1_seq}
    \seq_set_from_clist:cn {l__serge_line_##1_seq} {##2}
  }
  \int_set:Nn \l__serge_lines_int {\seq_count:N \l__serge_lines_seq}
}

\cs_new_protected:Npn \__serge_showlines:nn #1#2
{
  \int_set:Nn \l_tmpa_int {#1}
  \int_do_until:nn {\l_tmpa_int > #2} {
    \noindent Ligne~\int_to_arabic:n {\l_tmpa_int}: ~
    \seq_use:cn {l__serge_line_\int_to_arabic:n {\l_tmpa_int}_seq} {~}
    \par
    \int_incr:N \l_tmpa_int
  }
}
\cs_new_protected:Npn \__serge_findlinewith:n #1
{
  \int_step_inline:nn {\l__serge_lines_int}
  {
    \seq_if_in:cnT {l__serge_line_##1_seq} {#1}
    {
      \noindent Line~##1~contains~#1.\par
    }
  }
}
\cs_new_protected:Npn \__serge_set_currline:n #1
{
  \seq_set_eq:Nc \l__serge_currline_seq {l__serge_line_#1_seq}
}

\NewDocumentCommand \showlines {mm}
{
  \__serge_showlines:nn {#1} {#2}
}
\NewDocumentCommand \findlinewithx {m}
{
  \__serge_findlinewith:n {#1}
}
\NewDocumentCommand \SetLine {o} 
{
  \tl_if_novalue:nTF {#1}
  {
    \exp_args:Ne \__serge_set_currline:n {\int_rand:n {\seq_count:N \l__serge_lines_seq}}
  } {
    \__serge_set_currline:n {#1}
  }
}
\NewExpandableDocumentCommand \Element {som}
{
  \tl_if_novalue:nTF {#2}
  {
    \bool_if:NTF #1 
    {
      \seq_item:cn {l__serge_line_ \int_rand:n {\seq_count:N \l__serge_lines_seq} _seq} {#3}
    } {
      \seq_item:Nn \l__serge_currline_seq {#3}
    }
  } {
    \seq_item:cn {l__serge_line_#2_seq} {#3}
  }
}
\NewExpandableDocumentCommand \CalcWithTwo {smmm}
{
  \cs_set_nopar:Npn \__serge_tmp_calc:nn ##1##2 
  {
    \bool_if:NTF #1
    {
      \fp_eval:n {#4}
    } {
      \int_eval:n {#4}
    }
  }
  \__serge_tmp_calc:nn {\Element{#2}} {\Element{#3}}
}
\NewExpandableDocumentCommand \Evaluate {sm}
{
  \group_begin:
    \cs_set_eq:NN \E \Element
    \bool_if:NTF #1
    {
      \fp_eval:n {#2}
    } {
      \int_eval:n {#2}
    }
  \group_end:
}
\NewDocumentCommand \ProcessLines {+m} 
{
  \__serge_process_lines:n {#1}
}
\NewExpandableDocumentCommand \LinesCount {}
{
  \int_to_arabic:n {\l__serge_lines_int}
}
\NewExpandableDocumentCommand \ElementsCount {m}
{
  \int_to_arabic:n {\seq_count:c {l__serge_line_#1_seq}}
}
\NewExpandableDocumentCommand \CurrentCount {}
{
  \int_to_arabic:n {\seq_count:N \l__serge_currline_seq}
}
\ExplSyntaxOff
\ProcessLines{%
  {1,2,3,4,5,6,7,8,9},
  {10,11,12,13,14,15,16,17,18},
  {19,20,21,22,23,24,25,26,27},
  {28,29,30,31,32,33,34,35,36},
  {37,38,39,40,41,42,43,44,45},
  {46,47,48,49,50,51,52,53,54},
  {55,56,57,58,59,60,61,62,63},
  {64,65,66,67,68,69,70,71,72},
  {73,74,75,76,77,78,79,80,81}
  % {73,74,75,76,77,78,79,80,81x}% make sure non-int works
}
\newcount\totalelements
\newcount\linecounter
\newcount\elementcounter
\newcount\noelements
\begin{document}
\thispagestyle{empty}
\section*{Displaying Lines 1 to 9}
\showlines{1}{9}


\section*{Line with specific number}
\findlinewithx{5} 

\section*{Data access}
\SetLine[4]
\Element{3} % 3042
\Element[2]{5} % 14
\Element*{9} % random line, 9th element
\Element*{9} % random line, 9th element

\section*{Random line}
\SetLine % pick random line
\noindent Element 1: \Element{1};
Element 2: \Element{2};
Element 3: \Element{3};
Element 4: \Element{4};
Element 5: \Element{5};
Element 6: \Element{6};
Element 7: \Element{7};
Element 8: \Element{8};
Element 9: \Element{9}

\section*{Counting}
There are \LinesCount\ lines.
The current line contains \CurrentCount\ elements.
Line 4 contains \ElementsCount{4} elements.

\section*{Integer calculations}
\emph{Note integer calculations will sometimes error due to arithmetic 
overflows.}

\noindent Nos.\ Element 1 + Element 2: \inteval{\Element{1} + \Element{2}} 
\emph{or} \CalcWithTwo{1}{2}{#1+#2}\par
\noindent Nos.\ Element 1 / (2-Element 2): \CalcWithTwo{1}{2}{#1/(2-#2)} 
\emph{or} \Evaluate{\E{1}/(2-\E{2})}

\section*{Floating point}
\noindent Nos.\ Element 1 / (2-Element 2): \CalcWithTwo*{1}{2}{#1/(2-#2)}
\emph{or} \Evaluate*{\E{1}/(2-\E{2})}

\section*{Other ways}
\linecounter=0
\noelements=0
\totalelements=0
\loop
  \advance\linecounter by 1
  \SetLine[\the\linecounter]%
  \advance\noelements by \CurrentCount
  \elementcounter=0
  {%
    \loop
      \global\advance\elementcounter by 1
      \global\advance\totalelements by \Element{\the\elementcounter}\relax
    \ifnum\elementcounter < \CurrentCount 
    \repeat
  }%
\ifnum\linecounter < \LinesCount
\repeat
\noindent The total number of elements is \inteval{\noelements}.
The total of all elements is \inteval{\totalelements}.
The mean is \fpeval{\totalelements/\noelements}.
  
\end{document}

calculations

4
  • Yes ! thank you to you and to Explore. Just one question, can we make donations to those who help us on the site? Commented 13 hours ago
  • @Serge75013 no, not that I know of. some contributors have GitHub or similar pages and it is possible to donate there. here you can probably donate to SE, but they're a commercial company. the people actually helping (even the site moderators) are all volunteers. Commented 12 hours ago
  • OK... On the other hand, the element{1} etc variables output the numbers in string and I can't perform operations on them... How can I convert them to integers and perform for example the sum of the first three elements or their product? Commented 11 hours ago
  • 1
    @Serge75013 please see edit, but also please edit your question to ensure this is useful to future readers. (it can be reopened once the reasons for closure are fixed.) Commented 9 hours ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.