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

Need help with strings

Started by Marcono1234, 01 October 2014 - 01:01 PM
Marcono1234 #1
Posted 01 October 2014 - 03:01 PM
Hello, first it would be very kind if you could help me :)/>

My first question is, does only string.find, string.sub and string.gmatch works for computercraft or does the other string functions from lua also work (like string.gsub)?
My second question is, how do I only use the first "result" of a function that gives more than one back, example:
I want a string.find() and instantly calculate with the first result of string.find() another value, but string.find("Apple","a")*100 doesn't work

And then I have a problem, I want to have a function that gets some arguments and then uses them to set a given value and also adds it to a some kind of inverted table:

function inputTabel(tableName, entry, addInverted)
   entry = tableName
   if addInverted then
      --[[
      -- Tablename format: table1["table2"]["table3"]
      1. add [" and "] around the "table1"
      2. add an "inverted" in front of the tableName --> inverted["table1"]["table2"]["table3"]
      3. set the "table3" string as the value for inverted["table1"]["table2"]["entry"]
      -- So basically I want to get also the key for a value in a table without knowing the key but knowing the value: ["Test"] = 5
      -- I want then the key for the value 5
      ]]
   end
end
Maybe there is also a easier way to get this:
So basically I want to get also the key for a value in a table without knowing the key but knowing the value: ["Test"] = 5
I want then the key for the value 5
If so, I would really appreciate if you could tell me it

*Also by the way, there is a bug in this forum: When I activate for example "Italic", then write a line, deactivate it, press ENTER, write something else and then press backspace it acts like "Pos1", and this is only hapening here (Using a German Keyboard Layout)
Edited on 01 October 2014 - 01:29 PM
Bomb Bloke #2
Posted 01 October 2014 - 04:00 PM
My first question is, does only string.find, string.sub and string.gmatch works for computercraft or does the other string functions from lua also work (like string.gsub)?

Yes, they other string functions work. I've not come across one which doesn't.

My second question is, how do I only use the first "result" of a function that gives more than one back, example:
I want a string.find() and instantly calculate with the first result of string.find() another value, but string.find("Apple","a")*100 doesn't work

There's no lower-case "a" in "Apple", so that particular example would error with an "attempt to multiply nil by number" or some-such. I reckon it'd work if the search were valid, but even so, depending on your use-case you might be better off storing the result of string.find() in a variable, making sure that variable was set correctly, then do the math if so:

local pos = string.find("Apple","A")*100

if pos then
  print(pos*100)
else
  print("String search found no match.")
end

And then I have a problem, I want to have a function that gets some arguments and then uses them to set a given value and also adds it to a some kind of inverted table:
...
Maybe there is also a easier way to get this:
So basically I want to get also the key for a value in a table without knowing the key but knowing the value: ["Test"] = 5
I want then the key for the value 5
If so, I would really appreciate if you could tell me it

I guess you're talking about a reverse-lookup table?

Given that table keys don't have to be strings, we can use the pointer of the table we're adding to, as a key in your "inverted" table:

local inverted = {}

local function inputTabel(tableName, keyName, entry, addInverted)
	tableName[keyName] = entry

	if addInverted then
		if not inverted[tableName] then
			inverted[tableName] = {}
		end

		inverted[tableName][entry] = keyName
	end
end

local myTable = {}

inputTabel(myTable, "Test", 5, true)

print(myTable["Test"])      --> Prints 5

print(inverted[myTable][5]) --> Prints "Test"

Beware, however, if you try to set two indexes in "myTable" to the same value, the reverse-lookup table will run into problems! One way around this is a search-function:

local function reverseLookup(tableName, entry)
	local results = {}
	
	for key, value in pairs(tableName) do
		if value == entry then results[#results+1] = key end
	end
	
	return unpack(results)
end

local myTable = {["Test"] = 5, ["Test2"] = 5}

print(myTable["Test"])      --> Prints 5
print(myTable["Test2"])     --> Prints 5

print(reverseLookup(myTable, 5)) --> Prints "Test   Test2"
Marcono1234 #3
Posted 01 October 2014 - 04:14 PM
Ok thank you very much :)/>
I think this helps me a lot, but one question, the

local pos = string.find("Apple","A")*100
if pos then
  print(pos*100)
else
  print("String search found no match.")
end
Wouldn't work I guess because it gives you two ints: 1 1 (first for when the searched part starts, seconds for the end)
Or would it still work?
H4X0RZ #4
Posted 01 October 2014 - 04:27 PM
Ok thank you very much :)/>
I think this helps me a lot, but one question, the

local pos = string.find("Apple","A")*100
if pos then
  print(pos*100)
else
  print("String search found no match.")
end
Wouldn't work I guess because it gives you two ints: 1 1 (first for when the searched part starts, seconds for the end)
Or would it still work?

when a function returns multiple things you can store them like this:

local pos1,pos2 = string.find("Apple","A")

--Most people do something like this to mark "unneeded" returned values

local _,pos = string.find("Apple","A")
--	  ^
--	  | you see the underscore?
Edited on 01 October 2014 - 02:29 PM
Marcono1234 #5
Posted 01 October 2014 - 04:31 PM
Ok thank you very much :)/>
I think this helps me a lot, but one question, the

local pos = string.find("Apple","A")*100
if pos then
  print(pos*100)
else
  print("String search found no match.")
end
Wouldn't work I guess because it gives you two ints: 1 1 (first for when the searched part starts, seconds for the end)
Or would it still work?

when a function returns multiple things you can store them like this:

local pos1,pos2 = string.find("Apple","A")

--Most people do something like this to mark "unneeded" returned values

local _,pos = string.find("Apple","A")
--	  ^
--	  | you see the underscore?
Thank you very much, that answered all of my questions :)/>
Bomb Bloke #6
Posted 01 October 2014 - 04:37 PM
Sorry, I put the "*100" in that code twice by accident. I was going for this:

local pos = string.find("Apple","A")

if pos then
  print(pos*100)
else
  print("String search found no match.")
end

But yeah, so long as the search is valid, you can just do this:

print(string.find("Apple","A")*100)
Marcono1234 #7
Posted 02 October 2014 - 06:54 PM
I have another question, I have the problem, that I want to have a function which reads a string and then a for loop should set the string to a table (hope you understand what I mean, so I want that the for loop can read it as a table) and also the other way :)/> Is there a way to solve this very simple?
KingofGamesYami #8
Posted 02 October 2014 - 08:44 PM
So, you want to break a string into it's parts? I would use string.gmatch for this.


for character in str:gmatch( "." ) do
  --#do stuff
end
Marcono1234 #9
Posted 02 October 2014 - 09:24 PM
So, you want to break a string into it's parts? I would use string.gmatch for this.


for character in str:gmatch( "." ) do
  --#do stuff
end
No :)/> My new question is, how to tell the system that it should interprete a string as a table name
KingofGamesYami #10
Posted 02 October 2014 - 09:27 PM
-snip-
No :)/> My new question is, how to tell the system that it should interprete a string as a table name
So you want to make a string into a table?

local function to_table( str )
  local tbl = {}
  for character in str:gmatch( "." ) do
    tbl[ #tbl + 1 ] = character
  end
end
Marcono1234 #11
Posted 02 October 2014 - 09:42 PM
-snip-
No :)/> My new question is, how to tell the system that it should interprete a string as a table name
So you want to make a string into a table?

local function to_table( str )
  local tbl = {}
  for character in str:gmatch( "." ) do
	tbl[ #tbl + 1 ] = character
  end
end
No, that was not what I meant, but still thanks :)/>
What I mean is

local function getTableKey(tableDirectory, entry, completeDirectory)
for key,value in pairs(tableDirectory) do
  if tostring(value) == tostring(entry) then
   if completeDirectory then
    return (tableDirectory.."[\""..key.."\"]")
   else
    return key
   end
  end
end
end
getTableKey("directions", "west", true)
Dragon53535 #12
Posted 02 October 2014 - 10:24 PM

return (tableDirectory.."[\""..key.."\"]")
--# Doesn't work. Use this
return tableDirectory[key]
The reason to use the bottom is that the []'s aren't part of a string and as such don't need to be in quotes. So really what you're doing is searching for the value to the key.
I'll show you with this

local tableList = {Key1 = "Pie?",["Key2"] = "Yes Pie is good!"}
local key = "Key2"
print(tableList["Key1"]) --#This prints out "Pie?"
print(tableList[key]) --#This prints out "Yes Pie is good!"
Edited on 02 October 2014 - 08:27 PM
Bomb Bloke #13
Posted 03 October 2014 - 02:15 AM
While it is possible to get the name of a variable holding a table pointer in string form, the fact that you'd want to makes me think you're doing something the hard way. What's the purpose of this?
Marcono1234 #14
Posted 11 October 2014 - 10:06 PM

return (tableDirectory.."[\""..key.."\"]")
--# Doesn't work. Use this
return tableDirectory[key]
The reason to use the bottom is that the []'s aren't part of a string and as such don't need to be in quotes. So really what you're doing is searching for the value to the key.
I'll show you with this

local tableList = {Key1 = "Pie?",["Key2"] = "Yes Pie is good!"}
local key = "Key2"
print(tableList["Key1"]) --#This prints out "Pie?"
print(tableList[key]) --#This prints out "Yes Pie is good!"
Alright thank you :)/>

I have changed the code to

local function getTableKey(tableDirectory, entry, completeDirectory)
for key,value in pairs(tableDirectory) do
  if tostring(value) == tostring(entry) then
   if completeDirectory then
    return tableDirectory[key]
   else
    return key
   end
  end
end
end

Now I always get the directory as return, this is great :)/>