Jump to content




[Lua][Questions]Help Needed Regarding Strings And Lists For Use In Api


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

#1 Frederikam

  • Members
  • 112 posts
  • LocationDenmark

Posted 24 January 2013 - 04:01 AM

I very recently got inspired by how binary works. Binary inspired me to make some kind of encryption API. It works that each symbol get a string of 20 symbols. "E" could for example be "aI19SæOj3ASd9hAMs81S" making it very hard to read. The API is supposed to come with two functions:

.encrypt(_string_)
.decrypt(_string_)

I just simply don't know how to: (I did try to find these before posting)
  • Make and use a list in Lua.
    • Make a list.
    • Get the size.
    • Refer to a variable in a list.
    • Remove a variable in the list.
    • Add a variable to the list.
  • Combine x amount of strings together to one.
  • Separate each symbol in a single string and put them into a list.
Help is very much appreciated.
-Frederikam

#2 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 24 January 2013 - 04:43 AM

well when you need a list you can just use a table, declare a table with
local mytable={}
and add values to it with
mytable["myindex"]="value"
or
mytable.myindex="value"

you can recall values with
print(mytable.myindex)
and it would priint "value".

generally we use tables with consecutive numbers as indexes so
local mytable={}
mytable[1]="first item in list"
mytable[2]="second item in list"

we can also use
table.insert(mytable,"value")
to insert "value" at the lowest empty number

to remove an item from the list use
mytable["myindex"]=nil
or
table.remove(mytable,"myindex")
the table.remove one will also move everything else down a number to fill the gap so for instance

mytable={}
mytable[1]="one"
mytable[2]="two"
mytable[3]="three"
table.remove(mytable,2)
print(mytable[2])
would print "three"

to get the number of items in a list use
print(#mytable)
and it will print the number of items in the table (only those with consecutive indexes starting at 1 will be counted)

if you have a table of strings and want to combine them all into one string use
local totstring=""
for num,sValue in ipairs(mytable) do
  totstring=totstring..sValue
end
print(totstring)

and to separate one string into a table of characters use
local mystring="hello world!"
local tChars={}
for i=1,#mystring do
  table.insert(tChars,string.sub(mystring,i,i))
end

if you have further questions (which I'm sure you will 'cos I suck at explaining things :D ) feel free to ask.

#3 Frederikam

  • Members
  • 112 posts
  • LocationDenmark

Posted 24 January 2013 - 09:57 PM

Thank you, it is very much appreciated, i just need some explanation on the following:

Is "myIndex" like a name, similar when you use the table.insert() gives the variable a number instead? And please explain how this works:

table.insert(tChars,string.sub(mystring,i,i))

#4 ikke009

  • Members
  • 224 posts
  • LocationSliding between sunbeams

Posted 24 January 2013 - 11:04 PM

"myIndex" is the place in the table (a number) where you insert something
so if you have randomtable = {}
randomtable[6] = 4
then the 6th slot in the table will have the value 4
while in table.insert() the first argument is the tablename, and the second argument is the value
so table.insert(randomtable, 8) will give the first available slot (in this case the first) the value 8
if you would now do randomtable[1] it will return you 8
if you would do randomtable[6] it will give you 4


table.insert(tChars,string.sub(mystring,i,i))
That will insert the result of string.sub(mystring,i,i) in the first available table slot in the tChars table.
The result of string.sub(mystring,i,i) is of course dependent on i and the string, but i trust you already knew that..
for instance

local mystring="hello world!"
local tChars={}
for i=1,#mystring do
  table.insert(tChars,string.sub(mystring,i,i))
end

That will make i go from 1 to #mystring (the number of characters in the string, in this case 12).
if i arrived at the point at which it is for instance 7, it will do the following
table.insert(tChars,string.sub(mystring,7,7))
so it inserts a value into the first available slot in the tChars table, and that value is the 7th character of mystring, or w

if you would do string.sub(mystring,7,9) it would give you the 7th till the 9th character of mystring, or wor
if you wold do string.sub(mystring,7) it would give you the 7th till the last character of mystring, or world!

#5 Frederikam

  • Members
  • 112 posts
  • LocationDenmark

Posted 24 January 2013 - 11:42 PM

Thank you for your explanation, I think that's all i need to make this API.

#6 ikke009

  • Members
  • 224 posts
  • LocationSliding between sunbeams

Posted 25 January 2013 - 01:13 AM

No problem, and good luck!

#7 Frederikam

  • Members
  • 112 posts
  • LocationDenmark

Posted 25 January 2013 - 11:55 PM

I seem to get a problem with this:
test={}
table.insert(test, "gsd")
print(test.1)
In this case I get:
input:3: ')' expected near '.1'


#8 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 26 January 2013 - 12:01 AM

I know test["1"] and test.1 is the same, but table.insert fills test[1], and not test["1"].

So use test[1].

#9 Frederikam

  • Members
  • 112 posts
  • LocationDenmark

Posted 26 January 2013 - 12:09 AM

Would;
print(test.tonumber(1))
also work instead?

#10 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 26 January 2013 - 12:15 AM

Nope. That would call a function stored in
test["tonumber"]
. But
test[tonumber(1)]
would work.

EDIT: tonumber(1) is still 1.

#11 Frederikam

  • Members
  • 112 posts
  • LocationDenmark

Posted 30 January 2013 - 09:48 PM

Is there any easy way to remove characters from a string? I know you can use string.sub() on everything else.
Also is there any easy way to do;
variable = variable + 1
similar to how you do;
variable++;
in Java?

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 30 January 2013 - 09:51 PM

you could use gsub to replace characters... gsub allows for an amount to replace to be set as well... read more on it here http://lua-users.org...LibraryTutorial

sadly there is no ++ operator.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users