+  HandyCache форум
|-+  Главная категория» English forum» Optimalization command to command inside
Имя пользователя:
Пароль:
Страниц: [1]   Вниз
  Отправить эту тему    Печать  
Автор Тема: Optimalization command to command inside  (Прочитано 2548 раз)
0 Пользователей и 1 Гость смотрят эту тему.
truefriend-cz
Постоялец
***

Репутация: +3/-1
Offline Offline

Сообщений: 118


« : 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
Сообщить модератору   Записан
zed
Постоялец
***

Репутация: +4/-0
Offline Offline

Сообщений: 141


« Ответ #1 : 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/
Сообщить модератору   Записан
Михаил
Gold beta tester
*****

Репутация: +337/-14
Offline Offline

Сообщений: 5513



« Ответ #2 : 07 августа 2017, 15:38:09 »

Код:
i = i:gsub('^%l', string.upper):gsub('_', ' ')
Сообщить модератору   Записан
zed
Постоялец
***

Репутация: +4/-0
Offline Offline

Сообщений: 141


« Ответ #3 : 07 августа 2017, 15:54:24 »

Михаил
This is not an optimization. Here is still 2 calls of "gsub".
Сообщить модератору   Записан
Михаил
Gold beta tester
*****

Репутация: +337/-14
Offline Offline

Сообщений: 5513



« Ответ #4 : 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".
Сообщить модератору   Записан
truefriend-cz
Постоялец
***

Репутация: +3/-1
Offline Offline

Сообщений: 118


« Ответ #5 : 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)
« Последнее редактирование: 07 августа 2017, 18:49:52 от truefriend-cz » Сообщить модератору   Записан
Михаил
Gold beta tester
*****

Репутация: +337/-14
Offline Offline

Сообщений: 5513



« Ответ #6 : 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"
Сообщить модератору   Записан
truefriend-cz
Постоялец
***

Репутация: +3/-1
Offline Offline

Сообщений: 118


« Ответ #7 : 07 августа 2017, 18:56:34 »

Михаил thank you very much.
Сообщить модератору   Записан
Страниц: [1]   Вверх
  Отправить эту тему    Печать  

 
Перейти в: