HandyCache форум

Главная категория => English forum => Тема начата: truefriend-cz от 07 Август 2017, 10:50:28



Название: Optimalization command to command inside
Отправлено: truefriend-cz от 07 Август 2017, 10:50:28
Hi. I optimalizing code and have question. I can join this two line to one? How?

i = 'image_for_show'

Код:
i = i:gsub('^%l', string.upper) -- first letter upper
i = i:gsub('%_', ' ') -- replace _ to space


Название: Re: Optimalization command to command inside
Отправлено: zed от 07 Август 2017, 14:01:25
I think that it's impossible to join this two operations to one and that you should ask such questions on https://stackoverflow.com/


Название: Re: Optimalization command to command inside
Отправлено: Михаил от 07 Август 2017, 15:38:09
Код:
i = i:gsub('^%l', string.upper):gsub('_', ' ')


Название: Re: Optimalization command to command inside
Отправлено: zed от 07 Август 2017, 15:54:24
Михаил
This is not an optimization. Here is still 2 calls of "gsub".


Название: Re: Optimalization command to command inside
Отправлено: Михаил от 07 Август 2017, 16:07:47
1. The question was: "I can join this two line to one? How?". This is optimization by code lines number that user asks.
2. And at the same time this is optimization by code execution: it removes medium temporary saves/loads from string variable "i".


Название: Re: Optimalization command to command inside
Отправлено: truefriend-cz от 07 Август 2017, 17:38:10
Михаил yes, it is great optimalization. Thank you.

And,.. I can have a condition that would also create a variable?
Equivalent for:
Код:
var = re.find(hc.url, [[\.(htm|html|php|php3|php5|asp|aspx)(\?|$)]], 1)
if var then
    hc.put_msg (1, 'Test ok: '..var)
end

Example idea:
url http://www.domain.tld/index.php
Код:
if re.find(hc.url, [[\.(htm|html|php|php3|php5|asp|aspx)(\?|$)]], 1) then
    --i dont know, maybe return? but i dont know parameters for return in this situation
end
To be output: php

--- --- ---
Example idea/solution from my coding, but not accepted in LUA:
Код:
if var=re.find(hc.url, [[\.(htm|html|php|php3|php5|asp|aspx)(\?|$)]], 1) then
    hc.put_msg (1, 'Test ok: '..var)
end
or
Код:
if var:re.find(hc.url, [[\.(htm|html|php|php3|php5|asp|aspx)(\?|$)]], 1) then
    hc.put_msg (1, 'Test ok: '..var)
end
or
Код:
if re.find(hc.url, [[\.(htm|html|php|php3|php5|asp|aspx)(\?|$)]], 1)>>var then
    hc.put_msg (1, 'Test ok: '..var)
end
or
Код:
for var in re.find(hc.url, [[\.(htm|html|php|php3|php5|asp|aspx)(\?|$)]]) do
    hc.put_msg (1, 'Test ok: '..var)
end
(sorry for maybe stupid idea/question)


Название: Re: Optimalization command to command inside
Отправлено: Михаил от 07 Август 2017, 18:49:19
Lua havn't such syntax. You should write:
Код:
local var = re.find(hc.url, [[\.(htm|html|php|php3|php5|asp|aspx)(\?|$)]], 1)
if var then
    hc.put_msg (1, 'Test ok: '..var)
end
or
Код:
if re.find(hc.url, [[\.(htm|html|php|php3|php5|asp|aspx)(\?|$)]]) then
    hc.put_msg (1, 'Test ok: '.. re.substr(1))
end

Note that you should use local variables almost everywhere.
"local var" is much preferred then "var"


Название: Re: Optimalization command to command inside
Отправлено: truefriend-cz от 07 Август 2017, 18:56:34
Михаил thank you very much.