Cycling through different file location options

Viewed 51

I'm essentially trying to create a function which tests the first location I give, in the form:

 myComputer.referenceLookup("/address/x/text")

and return the string in that location if it is not NULL or "None" or "" (empty).

If not, I want it to test the next possible location:

 myComputer.referenceLookup("/address/1/x/text")

Otherwise, I would like it to return an empty string ("").

I've tried looking in the Lua Manual to no avail as well as testing different forms in repl.it, but unfortunately, I can't replicate a similar example as I usually do when testing.

function firstLine(x)

if myComputer.referenceLookup("/Address/ .. (x) .. /text") != NULL or "None" or "" then

    return myComputer.referenceLookup("/Address/ .. (x) .. /text")

elseif myComputer.referenceLookup("/Address/1/ .. (x) .. /text") !=  NULL or "None" or "" then

    return myComputer.referenceLookup("/Address/1/ .. (x) .. /text")

else

    return ""

end

end

myComputer.out.firstHouseNumber = firstLine(housenumber)

It's worth noting that the usual way I would reference the fact is as follows:

myComputer.out.firstHouseNumber= myComputer.referenceLookup("/Address/housenumber/text")

or

myComputer.out.firstHouseNumber= myComputer.referenceLookup("/Address/1/housenumber/text")

The platform I'm using doesn't throw errors, it just will return blank instead of running the lua script so I am unable to debug (hence usually using repl.it).

I know this makes it a bit of an abstract question, but if anyone knows how I can do what I am describing, it would be very much appreciated.

1 Answers

Assumptions

Looking at your answer, I will assume that

  1. myComputer.referenceLookup is defined somewhere else and works as intended (and not part of this question)
  2. NULL is also defined somewhere else and represents some sort of nil-value

Answer

The line

if myComputer.referenceLookup("/Address/ .. (x) .. /text") != NULL or "None" or "" then

doesn't work, because the or operator doesn't work that way.

How Lua interprets it is

if (myComputer.referenceLookup("/Address/ .. (x) .. /text") != NULL) or "None" or ""

and since "None" is a String value and thus considered truthy, the if condition will always evaluate to true, so it will always return the first location. Also, there is no != operator in Lua; it's ~= instead.

As for a solution, you essentially need three comparisons like this:

if myComputer.referenceLookup("/Address/" .. x .. "/text") ~= NULL
and myComputer.referenceLookup("/Address/" .. x .. "/text") ~= "None"
and myComputer.referenceLookup("/Address/" .. x .. "/text") ~= "" then

Obviously calling the function three times is a bad idea, both because of performance and because it may have side effects, so it's better to save it into a variable first like so:

local result = myComputer.referenceLookup("/Address/" .. (x) .. "/text")
if result ~= NULL and result  ~= "None" and result  ~= "" then
  return result
end

Extra

If you want to make your program easier to extend, you can also use string.format to build the locations from templates. Say you have a table containing all your locations like this:

local locations = {
  "/Address/%s/text";
  "/Address/1/%s/text";
}

Then you can iterate through the entries using ipairs and build each location using string.format:

for index, template in ipairs(locations) do
  local result = myComputer.referenceLookup(template:format(x))
  if result ~= NULL and result  ~= "None" and result  ~= "" then
    return result
  end
end

Note that you can write string.format(template, x) as template:format(x) as long as template is a string. (further reading)

Related