Lua comparison,numbers and keys tables
The file contains integers in decimal form. Convert each a number in binary form. Perform data encryption so that every eight binary characters turn into a letter of the Latin alphabet. The table of correspondence of codes to letters is contained in the second the file. Provide a function for performing decryption.
function tobin(num)
local tmp = {}
repeat
tmp[#tmp+1]=num%2
num = math.floor(num/2)
until num==0
return table.concat(tmp):reverse()
end
local function main()
io.input("number.txt")
local m = {}
for i = 1, 6 do
local val = io.read("*number")
if val == nil then break end
m[i] = tobin(val)
print(m[i])end
local a = {}
--local n = 1
io.input("shfr.txt")
while true do
local line = io.read("*line")
if line == nil then break end
x,y = string.match(line,"([01]+) (%a)")
a[x] = y
end
for k,v in pairs(a) do
print("["..k.."]="..v)
end
for i = 1, 6 do
if a[m[i]] then m[i] = y end
print(m[i])
end
io.close()
end
main()
Here I tried to compare binary numbers with a key and replace them with a letter that is in the value the key. but it only outputs F F F F F
for i = 1, 6 do
if a[m[i]] then m[i] = y end
print(m[i])
end
This is the table
[11001001]=F
[11001100]=D
[11001110]=A
[11110101]=C
[11111110]=E
[11010111]=B
а это m[i]
11001110
11010111
11110101
11001100
11111110
11001001
2 answers
if a[m[i]] then m[i] = y end
And what is the global variable y
?
There's the last value from the read loop shfr.txt. Replace with a[m[i]]
With the table proposed in this question, formatted in the same way as in your previous question, and with the input data voiced in this question, here is a simple code:
local a={}
for line in io.lines("таблица") do
local x,y=string.match(line,"([01]+) (%a)")
a[x]=y
end
for line in io.lines("входные.данные") do
print(a[line])
end
Will output what you probably expect:
A
B
C
D
E
F
Free recommendation: if you need to write a program in some programming language, and you do not want to learn even the basics of this language, then it is faster and easier not to ask to write you a program in pieces (once and twice), and then it is not very meaningful to try to put these pieces together, but immediately turn to some freelance exchange. good luck!