Jump to content




Generate numbers from strings

lua

  • You cannot reply to this topic
17 replies to this topic

#1 sploders101

  • Members
  • 52 posts

Posted 11 June 2013 - 05: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.

#2 sploders101

  • Members
  • 52 posts

Posted 11 June 2013 - 05: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.

#3 KillaVanilla

  • Members
  • 303 posts

Posted 11 June 2013 - 06: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.

#4 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 11 June 2013 - 06: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

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 11 June 2013 - 06:46 PM

Tables will let you use strings as keys. Please post your current code and any error messages you're receiving.

#6 sploders101

  • Members
  • 52 posts

Posted 11 June 2013 - 09: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 by superanonymous, 11 June 2013 - 09:22 PM.


#7 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 11 June 2013 - 09:28 PM

Is test a table? You need a table to add a index to.

#8 CoderPuppy

  • Members
  • 121 posts

Posted 11 June 2013 - 09: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


#9 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 11 June 2013 - 09:59 PM

im assuming hes running it from vanilla lua, CC (LuaJ being retarded) dosent have that detailed stack trace

#10 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 11 June 2013 - 10: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
>


#11 sploders101

  • Members
  • 52 posts

Posted 12 June 2013 - 07: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.

#12 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 12 June 2013 - 07: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.

#13 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 12 June 2013 - 07:54 AM

View Postsuperanonymous, on 12 June 2013 - 07:46 AM, said:

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.)

#14 sploders101

  • Members
  • 52 posts

Posted 12 June 2013 - 07:59 AM

WOW thanks for the fast replies. Anyway, here is my code for the client.
http://pastebin.com/L1HJAxxN
Spoiler


#15 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 June 2013 - 08:00 AM

View PostOrwell, on 12 June 2013 - 07:54 AM, said:

But for the record: it's textutils.deserialize and not textutils.unserialize.
Its what now? Is this a troll or just a `moment`? textutils.

#16 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 12 June 2013 - 08:05 AM

View Posttheoriginalbit, on 12 June 2013 - 08:00 AM, said:

View PostOrwell, on 12 June 2013 - 07:54 AM, said:

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..

#17 sploders101

  • Members
  • 52 posts

Posted 12 June 2013 - 08:09 AM

View PostOrwell, on 12 June 2013 - 08:05 AM, said:

View Posttheoriginalbit, on 12 June 2013 - 08:00 AM, said:

View PostOrwell, on 12 June 2013 - 07:54 AM, said:

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

#18 CoderPuppy

  • Members
  • 121 posts

Posted 12 June 2013 - 08: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

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






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users