简体   繁体   中英

Assistance with Lua functions

As noted before, I'm relatively new to lua, but again, I learn quick. The last time I got help here, it helped me immensely, and I was able to write a better script. Now I've come to another question that I think will make my life a bit easier. I have no clue what I'm doing with functions, but I'm hoping there is a way to do what I want to do here. Below, you'll see an example of code I have to do to strip down some unneeded elements. Yeah, I realize it's not efficient in the least, so if anyone else has a better idea of how to make it much more efficient, I'm all ears. What I would like to do is create a function with it so that I can strip down whatever variable with a simple call of it (like stripdown(winds)). I appreciate any help that is offered, and any lessons given. Thanks!

winds = string.gsub(winds,"%b<>","")  
winds = string.gsub(winds,"%c"," ")  
winds = string.gsub(winds,"  "," ")  
winds = string.gsub(winds,"  "," ")  
winds = string.gsub(winds,"^%s*(.-)%s*$", "%1)")  
winds = string.gsub(winds,"&nbsp;","")  
winds = string.gsub(winds,"/ ", "(")  

Josh

This should be slightly better:

function stripdown(str)
    return (str:gsub("%b<>","")  
               :gsub("[%c ]+"," ")  
               :gsub("^%s*(.-)%s*$", "%1)")  
               :gsub("&nbsp;","")  
               :gsub("/ ", "("))
end

Reduced 3 of the patterns down to one; The brackets around the return expression reduce the output to only the first return value from gsub.

For a function like this, I'm a huge fan of object syntax:

function stripdown(winds)
    winds = winds:gsub("%b<>","")  
                 :gsub("%c"," ")  
                 :gsub("  "," ")  
                 :gsub("  "," ")  
                 :gsub("^%s*(.-)%s*$", "%1)")  
                 :gsub("&nbsp;","")  
                 :gsub("/ ", "(")  
    return winds
end

This version is no more or less efficient than the other, but there's a lot less syntactic noise and I find it easier to see what's going on.

There's a technical reason for not simply returning the big expression, and that's because gsub returns two results. Assigning it to winds "adjusts" away the (unwanted) second result, and the function returns only the string.

Turning it into a function is the easy part.

function stripdown(winds)
    winds = string.gsub(winds,"%b<>","")  
    winds = string.gsub(winds,"%c"," ")  
    winds = string.gsub(winds,"  "," ")  
    winds = string.gsub(winds,"  "," ")  
    winds = string.gsub(winds,"^%s*(.-)%s*$", "%1)")  
    winds = string.gsub(winds,"&nbsp;","")  
    winds = string.gsub(winds,"/ ", "(")  
    return winds
end

This function as written does produce and abandon a lot of intermediate string results, which can be a relatively expensive operation. It is almost certainly worth careful study of the documentation for string.gsub() and its pattern language . It should be possible to do at least some of what you've specified in fewer operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM