[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to prevent ..., function asText improved
- From: Tomas Guisasola Gorham <tomas@...>
- Date: Mon, 26 Apr 1999 09:30:53 -0300 (EST)
Jon,
your solution doesn't work when a function is the index of a table
neither when a string with a space (for instance) in it is the index.
You can solve this by eliminating the 'asIndex' parameter and
forcing the characters '[' and ']' at the left side of the assignment.
I also take the liberty to change your repeat-until loop to a while-do-end
to eliminate a redundant test inside (if k ~= nil ...).
function asText(obj)
visitRef = {}
visitRef.n = 0
asTxRecur = function(obj)
if type(obj) == "table" then
if visitRef[obj] then
return "@"..visitRef[obj]
end
visitRef.n = visitRef.n +1
visitRef[obj] = visitRef.n
local begBrac, endBrac = "{", "}"
local t = begBrac
local k, v = next(obj, nil)
while k do
if t > begBrac then
t = t..", "
end
t = t.."["..asTxRecur(k).."]="..asTxRecur(v)
k, v = next(obj, k)
end
return t..endBrac
else
if type(obj) == "string" then
return '"'..obj..'"'
else
return tostring(obj)
end
end
end -- asTxRecur
return asTxRecur(obj)
end -- asText
Also, you couldn't distinguish two different tables with the same
contents. As you are numbering each table you write, you can change 'begBrac'
to "{@"..visitRef.n and you will be printing each table with a number.
After that you can write every reference to each table (despite it is a
circular reference) with its number and the mentioned difference will be
shown.
I made a function like that (about a year or two), but as my
problem doesn't have circular references I have never tried to resolve it.
On the other side, I made some effort to indent tables. If you are interested
in it, I'm merging both functions and can send you a copy.
Tomas