Lua remove characters from left - string

I am new to lua and i am trying to extract the value form the right side of a splited string. I have this:
local t ={}
local data = ("ret=OK,type=aircon,reg=eu,dst=1,ver=2_2_5,pow=1,err=0,location=0,name=,icon=0,method=home only,port=30050,id=,pw=,lpw_flag=0,adp_kind=2,pv=0,cpv=0,led=1,en_setzone=1,mac=FCDBB382E70B,adp_mode=run")
for word in string.gmatch(data, '([^,]+)') do
t[#t + 1] = word
end
local first = t[1]:gsub("%s+", "")
This gives me the string "ret=OK".
What can i do so that from this string to only get "OK", something like: get all from right of the equal sign and remove it and the left part. And this i must do for all the strings from "data" variable. Thank you.

I would recommend the following:
local data = 'ret=OK,type=aircon,reg=eu,dst=1,ver=2_2_5,pow=1,err=0,location=0,name=,icon=0,method=home only,port=30050,id=,pw=,lpw_flag=0,adp_kind=2,pv=0,cpv=0,led=1,en_setzone=1,mac=FCDBB382E70B,adp_mode=run'
local t = {}
for key, val in string.gmatch(data, '([^=,]*)=([^=,]*),?') do
t[key] = val
end
print(t['ret']) -- prints "OK"
print(t['adp_mode']) -- prints "run"
Note that the lua pattern makes the trailing comma optional (otherwise you miss the last keypair in the list).

Try this
for key, val in string.gmatch(data, "(%W*)=(%W*),") do
print(key.." equals "..val)
end

Related

groovy iterate through list of key and value

I have this list:
service_name_status=[a-service=INSTALL, b-service=UPGRADE, C-service=UPGRADE, D-service=INSTALL]
And I need to iterate through this list so the first element will be the value of a parameter called "SERVICE_NAME" and the second element will be the value of a parameter called "HELM_COMMAND",
after asserting those values to the parameters I will run my command that uses those parameters and then continue the next items on the list which should replace the values of the parameters with items 3 and 4 and so on.
So what I am looking for is something like that:
def service_name_status=[a-service=INSTALL, b-service=UPGRADE, C-service=UPGRADE, D-service=INSTALL]
def SERVICE_NAME
def HELM_COMMAND
for(x in service_name_status){
SERVICE_NAME=x(0,2,4,6,8...)
HELM_COMMAND=x(1,3,5,7,9...)
println SERVICE_NAME=$SERVICE_NAME
println HELM_COMMAND=$HELM_COMMAND
}
the output should be:
SERVICE_NAME=a-service
HELM_COMMAND=INSTALL
SERVICE_NAME=b-service
HELM_COMMAND=UPGRADE
SERVICE_NAME=c-service
HELM_COMMAND=UPGRADE
SERVICE_NAME=d-service
HELM_COMMAND=INSTALL
and so on...
I couldn't find anything that takes any other element in groovy, any help will be appreciated.
The collection you want is a Map, not a List.
Take note of the quotes in the map, the values are strings so you need the quotes or it won't work. You may have to change that at the source where your data comes from.
I kept your all caps variable names so you will feel at home, but they are not the convention.
Note the list iteration with .each(key, value)
This will work:
Map service_name_status = ['a-service':'INSTALL', 'b-service':'UPGRADE', 'C-service':'UPGRADE', 'D-service':'INSTALL']
service_name_status.each {SERVICE_NAME, HELM_COMMAND ->
println "SERVICE_NAME=${SERVICE_NAME}"
println "HELM_COMMAND=${HELM_COMMAND}"
}
EDIT:
The following can be used to convert that to a map. Be careful, the replaceAll part is fragile and depends on the data to always look the same.
//assuming you can have it in a string like this
String st = "[a-service=INSTALL, b-service=UPGRADE, C-service=UPGRADE, D-service=INSTALL]"
//this part is dependent on format
String mpStr = st.replaceAll(/\[/, "['")
.replaceAll(/=/, "':'")
.replaceAll(/]/, "']")
.replaceAll(/, /, "', '")
println mpStr
//convert the properly formatted string to a map
Map mp = evaluate(mpStr)
assert mp instanceof java.util.LinkedHashMap

Get part of string after a whitespace

So let's say I have a string called Hi there.
I am currently using
m:match("^(%S+)")
to get just Hi from the string, now all I need to do is just get "there" from the string but I have no idea how.
Checkout this page: http://lua-users.org/wiki/SplitJoin
There are numerous ways to split words in a string on whitespace.
This one seems like it might be a good fit for your problem:
function justWords(str)
local t = {} -- create an empty table
-- create a function to insert a word into the table
local function helper(word) table.insert(t, word) return "" end
-- grab each word in the string and pass it to `helper`
if not str:gsub("%w+", helper):find"%S" then return t end
end
table = justWords(example)
table[1] -- hi
table[2] -- there
table[3] -- nil

SELECT part of string between symbol and space

I need to create a subquery (or view) column with values pulled from part of a long string. Values will appear like this:
"Recruiter: Recruiter Name Date:..."
I need to select the recruiter name after : and end with the space after the recruiter name. I understand that normalizing would be better, but we only have query access not database setup access in this case.
Ideas appreciated!
You can use a regex for this. A regex will let you express that you want to search for the text Recruiter followed by a colon, a space, and a series of characters followed by a space, and that you want it to extract those characters.
The expression might look a bit like this (untested)
Recruiter: (.+) Date:
This would look for 'Recruiter: ' literally, followed by a string of any characters (.) of length 1 or larger (+), which is to be extracted (the brackets), followed by the literal string ' Date:'.
How you use this with SQL depends on your vendor.
I would create a function that pulls out the value for a given key. You would use it like:
select [dbo].[GetValue]('recruiter',
'aKey: the a value Recruiter: James Bond cKey: the c value')
This returns 'James Bond'
Here is the function:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[GetValue](#Key varchar(50), #Line varchar(max))
returns varchar(max)
as
begin
declare #posStart int, #posEnd int
select #posStart=charindex(#Key, #Line) -- the start of the key
if(#posStart = 0)
return '' -- key not found
set #posStart = #posStart + len(#Key) + 1 -- the start of the value
select #line = substring(#line, #posStart, 1000) -- start #Line at the value
select #posEnd=charindex(':', #line) -- find the next key
if(#posEnd > 0)
begin
-- shorten #line to next ":"
select #line = substring(#line, 0, #posEnd)
-- take off everything after the value
select #posEnd = charindex(' ', reverse(#line));
if(#posEnd > 0)
select #line = substring(#line, 0, len(#line) - #posEnd + 1)
end
return rtrim(ltrim(#line))
end
go

Find index of a specific character in a string then parse the string

I have strings which looks like this [NAME LASTNAME/NAME.LAST#emailaddress/123456678]. What I want to do is parse strings which have the same format as shown above so I only get NAME LASTNAME. My psuedo idea is find the index of the first instance of /, then strip from index 1 to that index of / we found. I want this as a VBScript.
Your way should work. You can also Split() your string on / and just grab the first element of the resulting array:
Const SOME_STRING = "John Doe/John.Doe#example.com/12345678"
WScript.Echo Split(SOME_STRING, "/")(0)
Output:
John Doe
Edit, with respect to comments.
If your string contains the [, you can still Split(). Just use Mid() to grab the first element starting at character position 2:
Const SOME_STRING = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(Split(SOME_STRING, "/")(0), 2)
Your idea is good here, you should also need to grab index for "[".This will make script robust and flexible here.Below code will always return strings placed between first occurrence of "[" and "/".
var = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(var, (InStr(var,"[")+1),InStr(var,"/")-InStr(var,"[")-1)

String Pattern Matching/Finding/Counting/Replacing

So this is a robust problem. I have a function which accepts 2 args (string_name, macros). Here it is so I can further explain.
function ParseStrings(string_name, macros)
return my_table[string_name]
-- All it does it returns the string_name's value
end
The problem is that the second arg is a table, and if it's a table then in the string there are going to be various parts that have the format "String stuff $MACRO_KEY; more string text" and the content between the $ and ; is the key to look up in the macro table sent with it. Now anytime a value like that appears in the string there will always be a second arg that's a table, so no problems their. I need to be able to count up the number of instances of macros in a string and then replace each macro component with it's respective macros' table value. So here's how the func is called in this instance...
local my_table = {
my_string = "My string content $MACRO_COMPONENT; more string stuff $MACRO_COMPONENT_SUB;$MACRO_COMPONENT_ALT;"
}
local macro = {
MACRO_COMPONENT = "F",
MACRO_COMPONENT_SUB = "Random Text",
MACRO_COMPONENT_ALT = "14598"
}
function ParseStrings(string_name, macros)
return my_table[string_name]
-- All it does it returns the string_name's value
end
ParseStrings("my_string", macro)
So I am thinking:
string.gsub(my_table[my_string]:match("%b$;"):sub(2,my_table[my_string]:match("%b$;"):len() - 1)
but this is a long and overtly complex answer (AFAIK) and from my tests it only does 1 replacement (because the pattern is only found once) and that's doesn't work well if there are multiple instances in the string. So ideas?

Resources