Author Topic: lua string compare  (Read 251 times)

Offline kyle.dawson

  • Full Member
  • ***
  • Posts: 153
  • Karma: +1/-1
lua string compare
« on: January 14, 2019, 02:23:16 pm »
Looking to see best way to compare strings.


local EVENT_MESSAGE = "Good Washing Everyone"
if (string.starts(EVENT_MESSAGE,"Good")) then
    return true
else
    return false
end


But I do not think the function string.starts works in Vera.  I can use string.len and string.format, but does not look like "starts" or "ends" function work.

https://docs.coronalabs.com/guide/data/luaString/index.html

Does anyone know best way to see if two strings are the same or contain a word?

VeraPlus(1), Sonos(3), GE Switch(30), GE Dimmer(5), GE Zwave Outlet(3), MimoLight Garage Door(1),Neo Motion(6), Door Sensors(3) Camera(9), Honeywell Thermostat(2), Google Home(4), Alexa(4), Google Chrome Cast(5)  - Driven via ALTUI

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6384
  • Karma: +290/-70
  • "Less is more"
Re: lua string compare
« Reply #1 on: January 14, 2019, 02:45:25 pm »
For pure equality, just use the == operator.

For partial matches, use string.match() or string.find(), see

https://www.lua.org/manual/5.1/manual.html#pdf-string.match

You'll need to master Lua pattern strings (really a subset of regular expressions.)

https://www.lua.org/manual/5.1/manual.html#pdf-string.upper

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline kyle.dawson

  • Full Member
  • ***
  • Posts: 153
  • Karma: +1/-1
Re: lua string compare
« Reply #2 on: January 14, 2019, 03:31:38 pm »
Thanks I will use this.

local EVENT_MESSAGE = "Good Morning"
local RESULT = string.match (EVENT_MESSAGE, "Morning" )
if ( RESULT == nil)  then
    return "Did not Find"
else
    return "Found result"
end

Was thinking we could use more of lua and this seems common, but I guess not.

VeraPlus(1), Sonos(3), GE Switch(30), GE Dimmer(5), GE Zwave Outlet(3), MimoLight Garage Door(1),Neo Motion(6), Door Sensors(3) Camera(9), Honeywell Thermostat(2), Google Home(4), Alexa(4), Google Chrome Cast(5)  - Driven via ALTUI

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6384
  • Karma: +290/-70
  • "Less is more"
Re: lua string compare
« Reply #3 on: January 14, 2019, 04:05:59 pm »
Sorry, don't understand.  This is all Lua.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline kyle.dawson

  • Full Member
  • ***
  • Posts: 153
  • Karma: +1/-1
Re: lua string compare
« Reply #4 on: January 14, 2019, 06:02:51 pm »
My goal was to use a standard lua function like this:

string.starts()

But that function does not appear to work or is not available in vera lua. When I run it it does not work and gives error.

Goal was to have function that returned true or false.  Based on your comments I decided to use another function that is in vara lua. 

Using the string.match() function, I can achieve same end goal.  Look at string and see if a sub string exist with in it. 

From what I can tell, vera lua is not 100% same as core lua.  Not big deal, but the reason I posted question.

VeraPlus(1), Sonos(3), GE Switch(30), GE Dimmer(5), GE Zwave Outlet(3), MimoLight Garage Door(1),Neo Motion(6), Door Sensors(3) Camera(9), Honeywell Thermostat(2), Google Home(4), Alexa(4), Google Chrome Cast(5)  - Driven via ALTUI

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1103
  • Karma: +183/-3
Re: lua string compare
« Reply #5 on: January 14, 2019, 06:59:04 pm »
My goal was to use a standard lua function like this:

string.starts()

But that function does not appear to work or is not available in vera lua. When I run it it does not work and gives error.

Goal was to have function that returned true or false.  Based on your comments I decided to use another function that is in vara lua. 

Using the string.match() function, I can achieve same end goal.  Look at string and see if a sub string exist with in it. 

From what I can tell, vera lua is not 100% same as core lua.  Not big deal, but the reason I posted question.

I think what akbooer is trying to convey is that Vera Lua is real Lua (Lua 5.1), and string.starts() is not real Lua, it's a Corona extension.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3, Lite. Hassio, Slapdash.

Offline kyle.dawson

  • Full Member
  • ***
  • Posts: 153
  • Karma: +1/-1
Re: lua string compare
« Reply #6 on: January 14, 2019, 07:06:30 pm »
Oh, I see now.  Good point.  Did not see that before.  Thanks for clarification.
VeraPlus(1), Sonos(3), GE Switch(30), GE Dimmer(5), GE Zwave Outlet(3), MimoLight Garage Door(1),Neo Motion(6), Door Sensors(3) Camera(9), Honeywell Thermostat(2), Google Home(4), Alexa(4), Google Chrome Cast(5)  - Driven via ALTUI