lua-users home
lua-l archive

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


With all your input I am able to solve it . Code looks as below , little bit modification in gsub arg format.

If any better solution... (from performance perspective) is really appreciated,


function modify_content_length(msg, length)

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

msg = "INVITE\r\nContent-Length : \r\n\r\nv=0\r\n"
modify_content_length(msg, string.len(msg))

 Thanks
Austin



On Thu, Jun 26, 2014 at 9:47 AM, Paige DePol <lual@serfnet.org> wrote:
On Jun 25, 2014, at 10:47 PM, Austin Einter <austin.einter@gmail.com> wrote:

> 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

You are not defining the variable `n` to be any value, hence the error message saying you are trying to concatenate a global `n`, which is a nil value. You need to assign a value to `n` before you can concatenate it, or use a different variable.

Might I suggest reading the documentation[1] and/or books to gain a better understanding of the language, especially the fundamentals like string handling and variable usage... it is worth the time, especially if you plan to use Lua for more than just this one script.

Also, please note that list netiquette is to avoid top-posting (posting your reply above the quoted text).

Good luck with your Lua scripting! :)

~pmd

[1] http://www.lua.org/docs.html