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

Generate numbers from strings

Started by sploders101, 11 June 2013 - 04:53 PM
sploders101 #1
Posted 11 June 2013 - 06:53 PM
I have tried everything and can't seem to find out how to do this:

I am making a banking program that uses a table to store the values. I would like for it to use their playernames as the index of the table but it won't let me do a string longer than 1 character. So I am looking for a way to convert a string into a number that can be decoded back into a string. Also I forgot to mention it uses openccsensors to get the playername securely. It has a server at the top of the building that stores the table into a file with textutils.serialize() but I can't get the table to work because it won't let me use strings. thank you for any help.
sploders101 #2
Posted 11 June 2013 - 06:55 PM
Oh I forgot to mention I would preferrably have it use string.byte so i can use string.char to convert it back. As long as I can convert it back I'm fine.
KillaVanilla #3
Posted 11 June 2013 - 07:08 PM
What error message do you get whenever you try to use a string as an index in a table?

As for your question: How long are the strings you're trying to convert to numbers? I have a method you could use in mind, but it can only hold a limited number of characters.
PixelToast #4
Posted 11 June 2013 - 07:28 PM
EDIT: thought you meant indexing a string not using string as index
you can index a string (i know, its weird but true)
for example:

for char in str:gmatch(".") do
works fine

as to the OP, you cannot put more than 3 or 4 characters into a single number
but you can use string.byte, why would you need to anyway?
the last time i recall needing it was to make an extremely inneficient encryption algorithm (i didnt know what i was doing)

if you want to make the string human unreadable, go look for a base64 implementation, i know StrUtils has one
Lyqyd #5
Posted 11 June 2013 - 07:46 PM
Tables will let you use strings as keys. Please post your current code and any error messages you're receiving.
sploders101 #6
Posted 11 June 2013 - 10:19 PM
This was just a test script I made to prove my point.


test["testing"]=0

ERROR:
line 1: attempt to index global 'test' (a nil value)
stack traceback:
	t.lua:1: in main chunk
	[C]: ?

Also PixelToast Your code seems like it might work but I have no clue how to use it lol. Also the strings would vary in length as I specified they are playernames.
Edited on 11 June 2013 - 08:22 PM
Sammich Lord #7
Posted 11 June 2013 - 10:28 PM
Is test a table? You need a table to add a index to.
CoderPuppy #8
Posted 11 June 2013 - 10:32 PM
You need to define the table first:

local test = {}
-- Your Code:
test["testing"] = 0

But I don't know how you got that error. I got:
test:2: index expected, got nil
PixelToast #9
Posted 11 June 2013 - 10:59 PM
im assuming hes running it from vanilla lua, CC (LuaJ being retarded) dosent have that detailed stack trace
Lyqyd #10
Posted 11 June 2013 - 11:20 PM
Works fine, you just have to do it correctly.


D:\Program Files (x86)\Lua\5.1>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> myTab = {}
> myTab.testing = 0
> myTab["testingtwo"] = 3
> print(myTab.testingtwo)
3
> print(myTab["testing"])
0
>
sploders101 #11
Posted 12 June 2013 - 08:46 AM
I am sending a table over rednet. I am using textutils.seialize and unserialize but whenever it downloads the table it acts as if it isn't declared.
LBPHacker #12
Posted 12 June 2013 - 08:51 AM
But how do you do that? Give us the code. There are plenty of ways of doing something wrong - and we are not mind readers, so there's no chance we can possibly figure out your problem by reading only something like "it acts as if it isn't declared". If you use the Lua prompt, tell us what you type in.
Orwell #13
Posted 12 June 2013 - 08:54 AM
I am sending a table over rednet. I am using textutils.seialize and unserialize but whenever it downloads the table it acts as if it isn't declared.
We need code, your description of the problem is extremely vague. But for the record: it's textutils.deserialize and not textutils.unserialize. (Where did I get that from? The wiki says I'm wrong.)
sploders101 #14
Posted 12 June 2013 - 08:59 AM
WOW thanks for the fast replies. Anyway, here is my code for the client.
http://pastebin.com/L1HJAxxN
Spoiler

os.loadAPI("/ocs/apis/sensor")
sens=sensor.wrap("bottom")
function getclosestplayer()
targets=sens.getTargets()
for k, v in pairs(targets) do
a=textutils.serialize(k)
a=string.gsub(a,"\"","")
if x==nil then return a end
--if 1==1 then break end
x=0
end
end
rednet.open("back")
while true do
player=getclosestplayer()
id,msg=rednet.receive()
if id==1740 then
rednet.send(1737,"get")
id2,msg2=rednet.receive()
if id2==1737 then
values=textutils.unserialize(msg2)
end
if values[player]~=nil then
values[player]=tonumber(msg)
end
if values[player]~=nil then
values[player]=values[player]+tonumber(msg)
end
rednet.send(1737,textutils.serialize(values))
end
print(values[player])
end
theoriginalbit #15
Posted 12 June 2013 - 09:00 AM
But for the record: it's textutils.deserialize and not textutils.unserialize.
Its what now? Is this a troll or just a `moment`? textutils.
Orwell #16
Posted 12 June 2013 - 09:05 AM
But for the record: it's textutils.deserialize and not textutils.unserialize.
Its what now? Is this a troll or just a `moment`? textutils.
It was a very weird moment. :P/> I changed my post allready. My brain must have been malfunctioning..
sploders101 #17
Posted 12 June 2013 - 09:09 AM
But for the record: it's textutils.deserialize and not textutils.unserialize.
Its what now? Is this a troll or just a `moment`? textutils.
It was a very weird moment. :P/>/> I changed my post allready. My brain must have been malfunctioning..
According to your sig you might be able to help me better than anyone else as I am using the player sensor
CoderPuppy #18
Posted 12 June 2013 - 09:55 AM
What code is running on the other computers?

And the closest player thing won't work because the proximity detector can detect all mobs and I don't think they're returned in order of distance.
Also you don't need to do that whole thing with serializing k then stripping quotes, you can just return k.

Slightly fixed code:
Spoiler

os.loadAPI("/ocs/apis/sensor")
sens = sensor.wrap("bottom")
function getclosestplayer()
local targets = sens.getTargets()
for k, v in pairs(targets) do
  if v.Name == 'Player' then
   return k
  end
end
end
-- origSend = rednet.send
-- rednet.send = function(id, msg)
--  print('sending "', msg, '" to ', tostring(id))
--  return origSend(id, msg)
-- end
rednet.open("back")
while true do
local player = getclosestplayer()
local id, msg = rednet.receive()
-- print(id, ': ', msg)
if id == 1740 then
  rednet.send(1737, "get")
  local id2, msg2 = rednet.receive()
  print(id2, ': ', msg2)
  if id2 == 1737 then
   values = textutils.unserialize(msg2)
  end
  if values[player] == nil then
   values[player] = tonumber(msg)
  else
   values[player] = values[player] + tonumber(msg)
  end
  rednet.send(1737, textutils.serialize(values))
end
-- print(player)
-- print(values[player])
end

Fixes:
  • getclosestplayer only returns players not all mobs
  • The value is properly initialized