Problem to define a key and the associated value of a table from a variable

I'm sorry for the imprecision of my question but I couldn't find a way to describe my problem correctly.

I created a new package and in the "lua" part, I use two classes, one for points and the other for lines. Two tables z and L are associated to these classes. In the first one are stored the points and in the second the lines. For example for points z.a = point: new (1,2). This defines the point a whose affix (complex number) is 1+2i. In the table here the key is a and the value 1+2i . In the same way if a and b are defined, I can define a line with for example L.a__b = line: new (z.a,z.b). This is a bit more complicated because tables are stored in a table.

I may be wrong but I think my problem can be translated like this: If x=a and y=b then how do I define a function that gives me L.a__b = line: new (z.a,z.b), L.x__y etc. gives nothing.

The package can be found on this page tkz-elements. You will find in the archive the code of tkz-elements a readme and a small documentation and several examples. All this is still experimental!

Here is the more complete code:

\documentclass{article}
\usepackage{tkz-euclide}
\usepackage{tkz-elements}
\begin{document}
\parindent = 0pt

\begin{elements}
z.a = point: new (0, -1)
z.b = point: new (4, 2)
z.c = point: new (1, 2)
z.d = point: new (-1, 3)
L.a__b = line: new (z.a,z.b)
L.a__d = line: new (z.a,z.d)
L.d__b = line: new (z.d,z.b)

va = tostring(c__d)
for i in pairs(L) do
if i == va then
else
_, _,ft, sd = string.find( "c__d", "(.+)__(.+)" )
L["ft__sd"] = line: new (z.ft,z.sd)  -- wrong !
--   L[ft"__"sd] = line: new (z.ft,z.sd)--  is fine D. Carlisle
break
end
end

-- z.i =intersection_ll (L.a__b,L.c__d)
-- the next code is here to show the different values
for i,k in pairs(L) do
tex.print(tostring(k)..";"..tostring(i))
for u,v in pairs(k)
do
tex.print(tostring(v))
end
tex.print('\\\\')
end
\end{elements}

\begin{tikzpicture}
\tkzGetNodes

\tkzDrawPoints(a,b,c,d,i)
\tkzLabelPoints(a,b,c,d,i)
\end{tikzpicture}
\end{document}

Some explanations: 4 points are defined then 4 lines but I commented the last one L.c__d to show my problem. The code allows to get the intersection of two lines if they are defined. I would like to find a test to know if one of them is not defined and in this case create it automatically. Having commented L.c__d = line: new (z.c,z.d) this line is no longer defined. So I parse the expression representing this line with the string.find function and get ft and sd which point to c and d.

Question: How can I use ft and sd to get L.c__d = line: new (z.c,z.d)?

• The problem is independent of tkz-euclide which is only used for drawing. The macro tkzGetNodes allows to get the elements of the table z to make them Nodes. Mar 21, 2023 at 23:01
• L[ft .. "__" .. sd] = line: new (z[ft],z[sd]) I would guess, if I understand the question Mar 21, 2023 at 23:07
• @DavidCarlisle Thanks. I feel very stupid this time. I still have trouble manipulating string and variable. Mar 22, 2023 at 5:45
• @DavidCarlisle You can perhaps place your comment in answer that I can validate Mar 22, 2023 at 5:50

As you are indexing lines by a concatenation of the indices of the points you need

L[ft .. "__" .. sd] = line: new (z[ft],z[sd])

It might be simpler instead to model lines with a 2D array (table of tables) so instead of

L.c__d = line: new (z.c,z.d)
L[ft .. "__" .. sd] = line: new (z[ft],z[sd])

you could use

L["c"]["d"] = line: new (z.c,z.d)
L[ft][sd]   = line: new (z[ft],z[sd])
• Yes it would be a possibility to use a 2D array but I hesitate ... L.c__d is an experiment with the idea of being able to create an object on the fly that does not exist. That said, it is not very educational. The introduction of object programming should rather allow to have more readable and understandable programs. Currently I describe lines with L.cd, circles with C.ab and triangles with T.abc. I admit that I prefer z.a to z["a"]. Mar 22, 2023 at 9:48
• @AlainMatthes z.a is just syntax sugar for z["a"] so you can always use either form but you can only use the dot form for string constants so if the string is in a variable or the result of a function (as in this question) you have to use [ ] so if most access is of that form just using z.a in the cases you have a literal constant z["a"] can look strange. Mar 22, 2023 at 10:08
• yes I agree with your arguments. At first sight the problem is in my functions and not especially for the users but I will have to make a choice. Mar 22, 2023 at 12:14