1

I am not sure why this does not work:

-- split data into an array of chars
dataList = string.gmatch(data, ".")

-- edit char 5 DOES NOT WORK
dataList[5] = 0x66

-- Print out the data in hex
for chr in dataList do
  io.write(string.format("[%02x] ", string.byte(chr)))
end

So if I remove the line dataList[5] = 0x66 then this works ok. So I don't get why I can't modify the element 5. The error I get is even more confusing for me:

Error: main.lua:33: attempt to index global 'dataList' (a function value)
stack traceback:
        main.lua:33: in function 'update'
        [string "boot.lua"]:463: in function <[string "boot.lua"]:435>
        [C]: in function 'xpcall'

What does that mean? - how can I achieve this?

Really all I want to do is modify a specific character of a string - but in lua people are saying you can't do it because they are immutable. So my idea is to split the string into an array and then modify this and then turn it back into a string when I am done...

update

Thanks to hjpotter92 I now have:

dataList = {data:byte(1, data:len())}         
dataList[5] = 0x66    
if dataList then
  finalString = string.char(table.unpack(dataList))     -- <---- this does not work :(
  printStringAsHex("final", finalString)  
end

However I am struggling to turn this back into a string, I get the error:

Error: main.lua:34: attempt to call field 'unpack' (a nil value) stack traceback: main.lua:34: in function 'update' [string "boot.lua"]:463: in function <[string "boot.lua"]:435> [C]: in function 'xpcall'

how can I achieve this?

2
  • 1
    Did you check the documentation for string.gmatch, particularly the part about what it returns? Commented Aug 15, 2016 at 16:55
  • @user2357112 ah, ok I did look at the docs, but I missed the return type.... how the heck do I achieve what I want to do then? - I have been staring at endless examples, can't see how; I don't want to do some complex string substitution thing (I have see people doing that... I miss simple C-style char arrays : ( Commented Aug 15, 2016 at 17:00

1 Answer 1

3

You want to perhaps store the string as a table (arrays are actually tables in lua):

dataList = {data:byte(1, data:len())}
dataList[5] = 0x66

print( string.char(table.unpack(dataList)) )

update

Didn't want to write a separate answer, so I have added a complete working example for any Lua version based on all the great answers / feedback. This is just for reference in case someone else encounters similar issues...

unpack = unpack or table.unpack

data = string.char(0x42, 0x42, 0x43, 0x15, 0x034, 0x33, 0x48)
dataList = {data:byte(1, data:len())}
dataList[5] = 0x66
print(string.char(unpack(dataList)))
Sign up to request clarification or add additional context in comments.

14 Comments

In Lua 5.1 use plain unpack(dataList) instead of table.unpack(dataList) (the unpack function moved into the table module in Lua 5.2).
@code_fodder what siffiejoe said above ^. Love's engine is still probably on Lua 5.1.
@code_fodder You can also put a local unpack = unpack or table.unpack somewhere, and later, use unpack(dataList)
@code_fodder No problem. I've modified that further to reduce unnecessary checks etc.
ahhhh... I didn't think what you wrote in your comment was actual working code, i.e. local unpack = unpack or table.unpack I thought that was pseudo code, that's cool!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.