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

How Do I Combine Multiple Strings Together

Started by TR1T0N_, 26 August 2013 - 08:06 AM
TR1T0N_ #1
Posted 26 August 2013 - 10:06 AM

ans = 1
		str = read()
		a = string.len(str)
		for i = 1, #str do
	local c = str:sub(i,i)
		print(c)
	 enc = letters[c]
	 print(enc)
	 print(i)

end
end
Hello so this code is the part of my encryption program which takes a input and stores it in a variable (str). Then it splits the variable "str" into separate letters which are then encrypted using a table called "letters", which includes letters, numbers and special characters and a random string e.g. ["a"] = "GqBSW",. Then the bit i need help with is combining all of the encrypted string together and each encrypted string needs to be stored in a separate variable. But i dont know how long the input will be so i wouldnt know how many variables i need to store each encrypted string. Any help would be much appreciated
————————————————————————————————————————————————————————
fixed my problem by using files to store encrypted string and using the fs apis append feature
Lyqyd #2
Posted 26 August 2013 - 10:25 AM
Split into new topic.
Shaun #3
Posted 26 August 2013 - 07:54 PM
Writing to a file isn't a good way of joining strings together :P/>

Check out the lua-users page on tables.
str = "75382912590"
myTable = {} --defines a new empty table

for i=1, #str do
  table.insert(myTable, str:sub(i,i)) -- adds stuff to the end of the table
end

for i,v in ipairs(myTable) do
  print(i, ": ", v) -- prints table contents
end

—-
One thing with your code, you define a as the length of str, but then use #str later. Why not just use #str?
theoriginalbit #4
Posted 26 August 2013 - 08:19 PM
I would actually suggest that given your current code to in fact do the following

enc = enc..letters[c]
instead of

enc = letters[c]

As for the solution that Shaun provided it's not going to concatenate the strings… if you go with his method, and use tables, do the following…


local str = "2345676543"
local enc = {}

for i = 1, #str do
  table.insert(enc, letters[str:sub(i,i)])
end

print( table.concat(enc) )
the above method actually concatenated the table together into a single string… however I do advise against the method of using tables, just use your existing `enc` variable.