lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Thanks, I modified as per suggestion, now script looks as below

function modify_content_length(msg, length)

    local offset = string.find(msg, "Content-Length", 1, true)
    if(offset == nil) then
    else
        msg1 = msg:gsub("(Content%-Length%s*:%s*)\r","%1"..n.."\r")
        print("msg1 = ",msg1)
    end

end

msg = [=[INVITE\r\nContent-Length : \r\n\r\nv=0\r\n]=]

modify_content_length(msg, string.len(msg))


Looks now getting error because of n

Error ->

lua5.2: string_test.lua:7: attempt to concatenate global 'n' (a nil value)
stack traceback:
    string_test.lua:7: in function 'modify_content_length'
    string_test.lua:15: in main chunk
    [C]: in ?



Regards
Austin



On Thu, Jun 26, 2014 at 8:51 AM, Choonster TheMage <choonster.2010@gmail.com> wrote:


On 26 Jun 2014 12:14, "Austin Einter" <austin.einter@gmail.com> wrote:
>
> Hi Luiz
> The scriptlooks as below
>
>
> function modify_content_length(msg, length)
>
>     local offset = string.find(msg, "Content-Length", 1, true)
>     if(offset == nil) then
>     else
>         print("offset = " , offset)
>         print(s:gsub("(Content%-Length%s*:%s*)\r","%1"..n.."\r"))    
>     end
>
> end
>
> msg = [===["INVITE\r\nContent-Length:\r\n\r\nv=0\r\n]===]
>
> modify_content_length(msg, string.len(msg))
>
>
> When I execute I get below error
>
> lua5.2: string_test.lua:9: attempt to index global 's' (a nil value)
> stack traceback:
>     string_test.lua:9: in function 'modify_content_length'
>     string_test.lua:18: in main chunk
>     [C]: in ?
>
> Regards
> Austin
>

Replace s:gsub with msg:gsub (msg being the variable you want to call gsub on).

You'll also need to return the result of gsub if you want to use it outside of modify_content_length (strings are immutable, functions like gsub return new strings instead of modifying existing ones).

Regards,
Choonster