Help with Lua Features

As noted earlier, I'm relatively new to lua, but then again, I will find out quickly. The last time they helped me, it helped me a lot, and I was able to write a better script. Now I have come to another question, which, I think, will make my life easier. I have no idea what I am doing with functions, but I hope there is a way to do what I want to do here. Below you will see an example of the code I need to do to break some unnecessary elements. Yes, I understand that this is ineffective, so if someone even better knows 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 break any variable with a simple call (e.g. stripdown (winds)). I appreciate any help that is offered, and any lessons. Thank you

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

+4
source share
3 answers

Enabling it in 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 it is written, produces and leaves a lot of intermediate string results, which can be a relatively expensive operation. It is almost certainly worth a close look at the documentation of string.gsub () and its template language . It should be possible to do at least part of what you specified with fewer operations.

+2
source

This should be a little better:

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

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

+4
source

For such a function, I am a huge fan of the 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 effective than the other, but there is much less syntax noise, and it’s easier for me to understand what is happening.

There is a technical reason for not just returning a large expression, but because gsub returns two results. Assigning it to winds "adjust" the (second) unwanted result, and the function returns only a string.

+3
source

Source: https://habr.com/ru/post/1306316/


All Articles