This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
augustas656's profile picture

Can you convert the name of a variable / object to a string?

Started by augustas656, 19 May 2014 - 04:19 PM
augustas656 #1
Posted 19 May 2014 - 06:19 PM
E.g.: abc = "lol"
and somehow by printing you can print "abc"?

This is so because I want to create a function that creates a key in an array with the name of a function which has the function in it. So I can access the function just by writing the table's name with the function's name after it. For example:

tbl = {
functions = {}
}
tbl.newFunc(inputfunc)
tbl[something] = inputfunc
end
function test()
print("lol")
end
a = tbl
a.newFunc(test)
a.functions.test
--Like that

Regards,
Augustas
viluon #2
Posted 19 May 2014 - 06:37 PM
To get the first key of the table:
local function getFirstKeyFromTable(table)
  for key in pairs(table) return key end
end
to get the key of the table by value ( getKeyByValueFromTable({a="b"},"b") returns "a")
local function getKeyByValueFromTable(tTable,value)
  local invertedTable={}
  for k,v in pairs(tTable) do
	invertedTable[v]=k
  end
  return invertedTable[value]
end

Edit: the code haven't been tested, I'll update ASAP
Edit2: updated
Edited on 19 May 2014 - 04:42 PM
augustas656 #3
Posted 19 May 2014 - 06:43 PM
That is not what I'm looking for. You see how you can call your function whatever you want, e.g.: function test(), I need to be able to use this function and get a string of the function's name, so I can call a function anything and insert it as an arguement into a function that will return me the string name like so: getName(test()) or something, and this should return not test() but "test" in a string. So the name.
Edited on 19 May 2014 - 04:51 PM
viluon #4
Posted 19 May 2014 - 06:46 PM
Well,
getName(test()) or something, and this should return not test() but "test" in a string
That is impossible AFAIK, you'd have to store the function in a table and know it's contents (that's doable with the code I posted I think). How do you want to look for the function? is it loaded with loadstring() or sth? btw, it's useless to get the name, you can write
test=function()  --#thats your function
print("asd")
end
temporaryFunction=test --#voila, you get the function under a name that you know
augustas656 #5
Posted 19 May 2014 - 06:53 PM
Look at the table above, at the start of my post.

Basically, I'd like a function which you input as an arguement, and this would return a string and the actual function, the string being what the function is called. This is beacuse I'd like a single arguement so that I can access this function by the table without having to use [1] or [2] etc, but instead the function's name like so array.func and it will run the function.
CometWolf #6
Posted 19 May 2014 - 06:58 PM
Just put the function into the table right away.

tFuncs = {
  test = function()
    print"test"
  end
}

If you really really want to do this silly and pointless thing your asking, here you go

function getName(variable)
  for k,v in pairs(getfenv()) do
    if v == variable then
	  return k
    end
  end
end
It's a silly waste of computing power, but it's doable in the case of functions and tables. However you'd be passing the function by knowing it's name in the first place, so…
viluon #7
Posted 19 May 2014 - 07:00 PM
Eh, okay. There's no way of getting the name of the function other than looking inside _G AFAIK Sorry, CometWolf is right because the name of the function is useless for the program, it serves just the user. You can therefore do something like
tSth={
  tFunctions={},
}
tSth.AddFunction=function(fnNew,sName)
  tSth.tFunctions[sName]=fnNew
end
tSth.AddFunction(test,"test") --#yes it's stupid but... tostring(function) would return sth like "function01245486"
Edited on 19 May 2014 - 05:01 PM
augustas656 #8
Posted 19 May 2014 - 07:32 PM
In my perspective this worth using, there are other reasons than to those I've already stated why I'd like to use only one arguement. But, I've asked is this possible, and how can I do this, now you've answered, if you want to speak about something else let's talk elsewhere. Thank you for your efforts in providing me the answer or any other actions that were relevant. I also don't believe this is that lag intense. My problem is solved.

Regards,
Augustas
Lyqyd #9
Posted 19 May 2014 - 07:58 PM
This is exactly the right place to address the underlying issue. What you are doing is roundabout and most likely unnecessary. If you better explain what you're trying to do and the chain of decisions that led to you deciding that this was remotely a good idea, we will be better able to point out a better solution.
augustas656 #10
Posted 19 May 2014 - 10:25 PM
My problem was just finding out how to convert the variable label name into a string. I don't have any other problems, and if telling why I want to use this is such a good idea, it's because I'm making a coroutine API of my own, and for basic reasons such as there is a less arguements to type if I didn't have the suggest the name of my own. And another reason is to prevent any possibly confusion if you mispell the name, or something. So mainly simple reasons, and I really don't mind using computer power, anyway as I've said, I really don't think I'm wasting so much computer power. I didn't have a problem but the problem with me trying to convert the variable name into a string. And, I don't want to ask everyone about everything I do, whetever it's the best way or there's a better, I only do so if I'm unsure, and right now I'm not, I want to get the variable name as a string.

Regards,
Augustas
CometWolf #11
Posted 19 May 2014 - 10:45 PM
The issue here is that since the function loops the environment table every time it's called, it will just get slower and slower for every variable you define. This will in the long run result in a very slow code, and since it relies on the environment table, it will not work with local variables or anonymous functions. Also for this to work through an API call, you might have to use getfenv(2) to get the program environment, instead of the API environment.
augustas656 #12
Posted 19 May 2014 - 11:06 PM
Well, I'm probably going to use the getObjectName function inside the API and make it usable for others who use the API outside the API's code. So like 1 and 2…
Lyqyd #13
Posted 19 May 2014 - 11:10 PM
What about anonymous or local functions? How do you plan on handling those?
augustas656 #14
Posted 19 May 2014 - 11:23 PM
Well then, how I set it up is that it will return nil if it can't find the key name. Well, if I find quite a big load of disadvantages I'll leave this as a simple function that I won't use inside my API but will be usable as an Extra addition to the API, and anyway my whole APIs is made of different functions that do diverse jobs. It's like a simple utility program that stuffs a lot of nice and compact features. Trying to get more and more ideas.