Replies: 6 comments 8 replies
-
Well, of course I went on trying and it worked after I changed the line "Tbl = {[i-4] = p}" to "Tbl[i-4] = p". Why it does work this way, I don't know. |
Beta Was this translation helpful? Give feedback.
-
@bijlevel If I was doing that, I would use table.insert() x=function(midi) local Tbl = {} -- is Tbl global? - if so declare Tbl outside function for i = 5, 10 do local t3 = BigInteger(midi:getData():getByte(i)) local p = t3:getBitRangeAsInt(0, 6) -- 6 bits, counting from the right to left. This effectively strips the first 2 bits table.insert(Tbl, p) console("p= " .. Tbl[i - 4]) end for j = 1, 5 do console("table value=: " .. Tbl[j]) end end :) |
Beta Was this translation helpful? Give feedback.
-
The reason for j =1,5 do console("table value=: "..Tbl[j]) end doesn't work is because each time you run Tbl = {[i-4] = p} The previous version of Tbl is destroyed on each iteration. With regard to Tbl.j this is accessing a hash based array, but by looping with an numeric iterator you can only access the index, not the key. |
Beta Was this translation helpful? Give feedback.
-
Here is an explanation of tables. There are two types hash key based and indexed. They can be mixed 0 a but ipairs will ignore the hash [0] for i,v in ipairs (t) do 1 b #t=3 not 4 --console=print -- uncomment for https://repl.it/new/lua ---[[ Tbl={1="dnaldoog",2="bijlevel",3="tedjuh",4="goodweather",5="BAUS"} -- not legal for i=1,5 do console(Tbl[i]) end --]] --lua: [string ""]:1: '}' expected near '=' --do this instead, but it is not necessary Tbl={[1]="dnaldoog",[2]="bijlevel",[3]="tedjuh",[4]="goodweather",[5]="BAUS"} for i=1,5 do console(Tbl[i]) end -- same as Tbl={"dnaldoog","bijlevel","tedjuh","goodweather","BAUS"} for i,v in ipairs(Tbl) do console(v) end --[[ dnaldoog bijlevel tedjuh goodweather BAUS --]] --Hash array returns nil except BAUS which is indexed Tbl={["1"]="dnaldoog",["2"]="bijlevel",["3"]="tedjuh",["4"]="goodweather",[5]="BAUS"} for i=1,5 do console(Tbl[i]) end --[[ nil nil nil nil BAUS --]] Tbl={["1"]="dnaldoog",["s2"]="bijlevel",["3"]="tedjuh",["4"]="goodweather",["5"]="BAUS"} or Tbl={["1"]="dnaldoog",s2="bijlevel",["3"]="tedjuh",["4"]="goodweather",["5"]="BAUS"} ["1"] distinguishes between 1 for k,v in pairs (Tbl) do console(k.." "..v) end --[[ 1 dnaldoog 3 tedjuh s2 bijlevel 5 BAUS 4 goodweather --]] -- note in lua hash order is not the same as written returns 1 3 2 5 4 console (Tbl[1]) -- nil console (Tbl.s2) -- bijlevel -- console (Tbl.1) -- lua doesn't like this!! |
Beta Was this translation helpful? Give feedback.
-
Try to do this in a memoryBlock if you can tempMB = MemoryBlock() as example. You can use all the getByte and getByteRange to pull things from MemoryBlocks. I think MemoryBlocks are more efficient, the downside being that you'll need to convert them to something to display in the console (using something like toHexString()) I try to keep all binary (hex) data in memory blocks if I can. |
Beta Was this translation helpful? Give feedback.
-
Same for me. Most of the sysex manipulation is done with Memoryblocks. |
Beta Was this translation helpful? Give feedback.
-
I'm a bit reluctant to post this "issue" because I'm probably overlooking something VERY obvious... I have this code (snippet):
for i = 5,10 do
t1 = midi:getData():getByte(i);
t3=BigInteger(t1)
p=t3:getBitRangeAsInt(0,6) -- 6 bits, counting from the right to left. This effectively strips the first 2 bits
Tbl = {[i-4] = p}
console("p= "..Tbl[i-4])
end
for j =1,5 do
console("table value=: "..Tbl[j])
end
Within the "i "loop the console does show the values stored in the table as expected, but when it executes the "j" loop Ctrlr throws an error and tells me I'm concatenating a "nil" value, meaning the table is empty! How is that possible or what am I doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions