Have to make a program for school. Basically you have a string and you need to make it secret by using a code. The secret string is each letter + the secret code in the alphabet. So BALL would be EDOO with the code 3. A space has location zero in the alphabet and if the location + the code is bigger than 26 the new letter is string + code - 26 now whenever I execute my program and add spaces it just "translates" the first part before the space. FYI, the comments next to it are in Dutch and not that important.
local code = 3
local string = string.upper("spaghetti carbonara") --string
local alfabet = " ABCDEFGHIJKLMNOPQRSTUVWXYZ" --alfabet
local lengte = 6 --lengte van string
for getal1 = 1, lengte do -- for want moet herhaald worden/getal
local letter = string.sub(string, getal1, getal1) -- letter uit de string halen
local locatie = string.find(alfabet, letter) -- letter vinden in alfabet
if locatie + code < 26 then
locatie2 = locatie + code -- nieuwe locatie vinden met code
elseif locatie + code > 26 then
locatie2 = locatie + code - 26
end
local letter2 = string.sub(alfabet, locatie2, locatie2) -- letter die overeenkomt met locatie
io.write(letter2)
end
First of all, you shouldn't be using the variable string like you're doing on line 2. That doesn't break your program only by coincidence because all strings have a metamethod with __index pointing to string. You should rename that variable to something else instead (text, input, str, etc.)
With that out of the way, your program seems to work just fine for me. Adding a bunch of spaces to the input adds a bunch of "C"s to the output. Maybe you've just forgotten to change that local lengte = 6 to local lengte = #string? As it currently is, it will only "translate" the first 6 characters in the string.
As a hint on how to improve this: you can replace the whole encryption / decryption part with a single call to string.gsub if you pass it a table as its third argument:
local code = {}
code["a"] = "b"
code["b"] = "a"
print(string.gsub("abc", ".", code))
-- Prints "bac"
-- "a" gets replaced with "b", as per the table
-- same with "b"
-- "c" does not appear as a key in the table, so it's simply ignored
Then all you need to do is build a table that maps every character you want to replace to what you want to replace it with, which is a bit easier to achieve:
local function decrypt_encrypt_dictionaries(alphabet, offset)
local encrypt, decrypt = {}, {}
-- Code that fills the two tables goes here :D
return encrypt, decrypt
end
local ROT13_encrypt, ROT13_decrypt = dectypt_encrypt_dictionaries("abcde"--[[...and so on]], 13)
local some_text = io.read()
-- Encrypt some text:
local encrypted = string.gsub(some_string, ".", ROT13_encrypt)
-- Decrypt it again:
local original = string.gsub(encrypted, ".", ROT13_decrypt)
-- See if it's the same:
print(original)
As I mentioned above, this only shifts the difficulty from the encryption/decryption to building the two Lua tables, which I'm leaving for you to figure out.
Related
I have an binary file with shows glibberish infos if i open it in Notepad.
I am working on an plugin to use with wireshark.
So my problem is that I need help. I am reading in an File and need to find 'V' '0' '0' '1' (0x56 0x30 0x30 0x31) in the File, because its the start of an Header, with means there is an packet inside. And I need to do this for the whole file, like parsing. Also should start the Frame with V 0 0 1 and not end with it.
I currently have an Code where I am searching for 0x7E and parse it. What I need is the length of the frame. For example V 0 0 1 is found, so the Length from V to the Position before the next V 0 0 1 in the File. So that I can work with the length and add it to an captured length to get the positions, that wireshark can work with.
For example my unperfect Code for working with 0x7E:
local line = file:read()
local len = 0
for c in (line or ''):gmatch ('.') do
len = len + 1
if c:byte() == 0x7E then
break
end
end
if not line then
return false
end
frame.captured_length = len
Here is also the Problem that the Frame ends with 7E which is wrong. I need something that works perfectly for 'V' '0' '0' '1'. Maybe I need to use string.find?
Please help me!
Thats an example how my file looks like if i use the HEX-Editor in Visual Studio Code.
Lua has some neat pattern tools. Here's a summary:
(...) Exports all captured text within () and gives it to us.
-, +, *, ?, "Optional match as little as possible", "Mandatory match as much as possible", "optional match as much as possible", "Optional match only once", respectively.
^ and $: Root to start or end of file, respectively.
We'll be using this universal input and output to test with:
local output = {}
local input = "V001Packet1V001Packet2oooV001aaandweredonehere"
The easiest way to do this is probably to recursively split the string, with one ending at the character before "V", and the other starting at the character after "1". We'll use a pattern which exports the part before and after V001:
local this, next = string.match(input, "(.-)V001(.*)")
print(this,next) --> "", "Packet1V001Packet2..."
Simple enough. Now we need to do it again, and we also need to eliminate the first empty packet, because it's a quirk of the pattern. We can probably just say that any empty this string should not be added:
if this ~= "" then
table.insert(output, this)
end
Now, the last packet will return nil for both this and next, because there will not be another V001 at the end. We can prepare for that by simply adding the last part of the string when the pattern does not match.
All put together:
local function doStep(str)
local this, next = string.match(str, "(.-)V001(.*)")
print(this,next)
if this then
-- There is still more packets left
if this ~= "" then
-- This is an empty packet
table.insert(output, this)
end
if next ~= "" then
-- There is more out there!
doStep(next)
end
else
-- We are the last survivor.
table.insert(output, str)
end
end
Of course, this can be improved, but it should be a good starting point. To prove it works, this script:
doStep(input)
print(table.concat(output, "; "))
prints this:
Packet1; Packet2ooo; aaandweredonehere
A string '321#322#323#324#325'.
here number of digits in each number is 3 but it's not limited to 3 it could be any number.
here are 5 numbers in a string but this number could be anything.
task is to get 321,322,323,324,325 and store in a table so that any operation could be performed over them.
I have tried several string functions like c = c:gsub('%W','') to eliminate those non-alphanumeric characters, but nothing helped.
function encrypter()--FUNCTION 14
c=' '
print('Please enter your message!')
local message=io.read()
lengthOfString=string.len(message)--Inbuit function to get length of a string.
newLine()
print('Please enter your key. Remember this key, otherwise your message wont be decrypted')
newLine()
key=io.read()
key=tonumber(key)
largeSpace()
print("Encrypted message is")
for s=1,lengthOfString do
--print(encryptionFormula(string.byte(message,s),key))
--inbuilt function for converting character of a string to it's respective ASCII value. First place gets character or variable having string whereas second place gets position of that character in the given string.
b=encryptionFormula(string.byte(message,s),key)
c=c..tostring(b)..'#'
--print(c)
b=0
end
print(c)
largeSpace()
print("Now share this message along with the key to the receiver. Don't share your key with anyone, if you don't want your message to be read.")
end
What you're looking for is string.gmatch().
local input = "123#546514#13#548#2315"
local numbers = {}
for number in string.gmatch(input, '%d+') do
table.insert(numbers, number)
end
-- Output the numbers
for index, number in ipairs(numbers) do
print(index, number)
-- This prints:
-- 1 123
-- 2 546514
-- 3 13
-- 4 548
-- 5 2315
end
If you don't know how Lua patterns work, you can read about them in the reference manual or you can have a look at Programming in Lua (the first edition is available for free on their website)
I need to find a way to see how many times an entry is listed in a table.
I have tried looking at other code for help, and looking at examples online none of them help
local pattern = "(.+)%s?-%s?(.+)"
local table = {"Cald_fan:1", "SomePerson:2", "Cald_fan:3","anotherPerson:4"}
for i,v in pairs(table) do
local UserId, t = string.match(v, pattern)
for i,v in next,UserId do
--I have tried something like this
end
end
it is suppose to say Cald_fan was listed 2 times
Something like this should work:
local pattern = "(.+)%s*:%s*(%d+)"
local tbl = {"Cald_fan:1", "SomePerson:2", "Cald_fan:3","anotherPerson:4"}
local counts = {}
for i,v in pairs(tbl) do
local UserId, t = string.match(v, pattern)
counts[UserId] = 1 + (counts[UserId] or 0)
end
print(counts['Cald_fan']) -- 2
I renamed table to tbl (as using table variable makes the table.* functions not available) and fix the pattern (you had unescaped '-' in it, while your strings had ':').
If the format of your table entries is consistent, you can simply split the strings apart and use the components as keys in a map of counters.
It looks like your table entries are formatted as "[player_name]:[index]", but it doesn't look like you care about the index. But, if the ":" will be in every table entry, you can write a pretty reliable search pattern. You could try something like this :
-- use a list of entries with the format <player_name>:<some_number>
local entries = {"Cald_fan:1", "SomePerson:2", "Cald_fan:3","anotherPerson:4"}
local foundPlayerCount = {}
-- iterate over the list of entries
for i,v in ipairs(entries) do
-- parse out the player name and a number using the pattern :
-- (.+) = capture any number of characters
-- : = match the colon character
-- (%d+)= capture any number of numbers
local playerName, playerIndex = string.match(v, '(.+):(%d+)')
-- use the playerName as a key to count how many times it appears
if not foundPlayerCount[playerName] then
foundPlayerCount[playerName] = 0
end
foundPlayerCount[playerName] = foundPlayerCount[playerName] + 1
end
-- print out all the players
for playerName, timesAppeared in pairs(foundPlayerCount) do
print(string.format("%s was listed %d times", playerName, timesAppeared))
end
If you need to do pattern matching in the future, I highly recommend this article on lua string patterns : http://lua-users.org/wiki/PatternsTutorial
Hope this helps!
I am pretty new to this, so I hope you can give me a hand.
I am programming lights, and what I like to do is take a variable from my lighting desk (a text string called "4 Mythos Stage") and split is into different variables.
to get the variables from the desk I use:
return function ()
local Layer1 = gma.user.getvar("Layer1") -- I placed "4 Mythos Stage" variable in Layer1
gma.feedback(Layer1) -- gives feedback 4 Mythos Stage
end
Now I would like to split the string into 3 new local variables named:
local number -- should produce 4
local fixturetype -- should produce Mythos
local location -- should produce Stage
i tried the following:
local number = string.match('Layer1', '%d+')
local fixturetype = string.match('Layer1', '%a+')
local location = string.match('Layer1', '%a+')
this didn't work, so can somebody please help me in the right direction. I would be really greatful.
with kind regards,
Martijn
You can assign all three variables at the same time, because Lua has multiple returns and multiple assignment. Put parentheses around each of your patterns in order to return them as captures, and combine them into a single pattern with spaces between them:
local number, fixturetype, location = string.match(Layer1, '(%d+) (%a+) (%a+)')
In case you will be using multiple spaces or tabs between the items, this pattern would be better:
local number, fixturetype, location = string.match(Layer1, '(%d+)[ \t]+(%a+)[ \t]+(%a+)')
The reason why your attempt didn't work is because string.match('Layer1', '%d+') is searching inside 'Layer1' (a string) instead of Layer1 (a variable).
But even if you corrected that, you would get 'Mythos' every time you called string.match(Layer1, '%a+') (where Layer1 == '4 Mythos Stage'). string.match always starts from the beginning of the string, unless you supply an index in the third parameter: string.match(Layer1, '%a+', 9) --> 'Stage'.
A robust solution for this task is to split the string into three "words", a word being a sequence of non-whitespace characters:
local number, fixturetype, location = string.match(Layer1, '(%S+)%s+(%S+)%s+(%S+)')
Could someone help me with this?
I can not make the variable "v" receive any characters while the variable "k" receives only alphanumeric.
t = {}
for k, v in string.gmatch(decrypt, "(%w+)=([^']*)") do
t[k] = v
print(k,v)
end
I have a file with the following content:
email=mbw#iue.com
ip=192.168.100.1
mac=af:45:t6:45:67
(maybe oversimplifying, if so, sorry...)
If you are trying to break the lines in your file at the '=' an assign them as key, value pairs in t:
--
-- PART I - read from a file
--
local file = "pattern.dat" -- our data file
local t = {} -- hold file values
for l in io.lines(file) do -- get one line at a time
local k, v = string.match(l, "(.+)=(.+)") -- key, value delimited by '=''
t[k] = v -- save in table
print(k,t[k])
end
print("\n\n")
--
-- PART II - read from data string
--
local data = "email=mbw#iue.com/ip=192.168.100.1/mac=af:45:t6:45:67"
data = data .. "/" -- need a trailing '/'
t = {} -- hold data values
for l in string.gmatch(data, "(.-)/") do -- get one 'line' at a time
local k,v = string.match(l, "(.+)=(.+)") -- key, value delimited by '=''
t[k] = v
print(k,t[k])
end
NOTE about the "^" anchor (from reference manual entry for 'gmatch'):
For this function, a caret '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.
http://www.lua.org/manual/5.3/manual.html#pdf-string.gmatch
You are actually only getting one match because * is greedy. If you are trying to split lines, try (%w+)=([^'\n]*)\n
Note: Lua uses patterns, not regular expressions. Sometimes the difference is unimportant, sometimes vital.