Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assistance with Lua functions

Tags:

function

lua

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

like image 313
Josh Avatar asked Jan 23 '23 07:01

Josh


1 Answers

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.

like image 100
daurnimator Avatar answered Jan 27 '23 22:01

daurnimator