used string.gsub to change white space to _ not working - lua

even if i use gsub to change white space to _ the if statement still get error attempt to index a nil value i wonder whats the problem.i cant use pairs since that's the instruction my teacher gave.
this is the code sorry in advance im a beginner.
text = "ib c e d f"
text = string.lower(text)
b = text:gsub("%s+", "_")
for k=1, #b do
if not string.sub(b, k,k) or string.sub(b, k,k) ~= " " then
if a[i][2] == string.sub(b, k,k) then
print(yes)
end
end

There are some syntax error on your code snippet which causes the error you mentioned. The gsub function is actually working just fine.
text = "ib c e d f"
text = string.lower(text)
b = text:gsub("%s+", "_") --> this gsub is working just fine
for k=1, #b do
if not string.sub(b, k,k) or string.sub(b, k,k) ~= " " then
if a[i][2] == string.sub(b, k,k) then --> you didn't assign any table called "a" before --> attempting to index a nil value
print(yes)
end
--> haven't close the "if not string.sub" function with an end
end --> this is the end for the loop "for k"
I am just wildly guessing that you want to compare the result string with the original string. Since your question is so enigmatic, I could only offer you this code below for your reference:
text = "ab c d e f "
text = string.lower(text)
output = text:gsub("%s", "_")
for k = 1, #output do
local char = string.sub(output, k, k)
local originalChar = string.sub(text, k, k)
if (originalChar ~= " ") and (char == originalChar) then
print(char .. " --> OK")
end
end
The gsub pattern uses %s instead of %s+ so that each space is converted to an underscore to allow simple unit test (char by char comparison). Code snippet available here.

Related

String with Numbers to Table

How can i transform this following string:
string = "1,2,3,4"
into a table:
table = {1,2,3,4}
thanks for any help ;)
Let Lua do the hard work:
s="1,2,3,4"
t=load("return {"..s.."}")()
for k,v in ipairs(t) do print(k,v) end
Below is the code adapted from the Scribunto extension to MediaWiki. It allows to split strings on patterns that can be longer than one character.
-- Iterator factory
function string:gsplit (pattern, plain)
local s, l = 1, self:len()
return function ()
if s then
local e, n = self:find (pattern, s, plain)
local ret
if not e then
ret = self:sub (s)
s = nil
elseif n < e then
-- Empty separator!
ret = self:sub (s, e)
if e < l then
s = e + 1
else
s = nil
end
else
ret = e > s and self:sub (s, e - 1) or ''
s = n + 1
end
return ret
end
end, nil, nil
end
-- Split function that returns a table:
function string:split (pattern, plain)
local ret = {}
for m in self:gsplit (pattern, plain) do
ret [#ret + 1] = m
end
return ret
end
-- Test:
local str = '1,2, 3,4'
print ('table {' .. table.concat (str:split '%s*,%s*', '; ') .. '}')
You can use gmatch and the pattern (%d+) to create an iterator and then populate the table.
local input = "1,2,3,4"
local output = {}
for v in input:gmatch("(%d+)") do
table.insert(output, tonumber(v))
end
for _,v in pairs(output) do
print(v)
end
The pattern (%d+) will capture any number of digits(0-9).
This is a narrow solution, it does not handle blank entries such as
input = "1,2,,4"
output = {1,2,4}
It also does not care what the delimitator is, or if it is even consistent for each entry.
input = "1,2 3,4"
output = {1,2,3,4}
Reference:
Lua 5.3 Manual, Section 6.4 – String Manipulation: string.gmatch
FHUG: Understanding Lua Patterns

How to get all diagonal from an array of words in Ruby?

I have an array in Ruby
words = ["horses", "follow", "useful", "offset"]
Reference:
h o r s e s
f o l l o w
u s e f u l
o f f s e t
I want to get a list of all its diagonals like this.
Here is want I expect in result:
["o", "uf", "fsf", "hoes", "olfe", "rlut", "sol", "ew", "s"]
Would be helpful if anyone can help me a bit on this. Thanks
Try that:
words = ["horses", "follow", "useful", "offset"]
words.reverse.each_with_index.map{|s,i| " " * i + s }.inject(Array.new(words.size + words.last.size-1,"")) do |a,s|
s.chars.each_with_index do |c,i|
a[i] = c + a[i]
end
a
end.map(&:strip)
# => ["o", "uf", "fsf", "hoes", "olfe", "rlut", "sol", "ew", "s"]
At first words.reverse.each_with_index.map{|s,i| " " * i + s } builds array with whitespace offset:
offset
useful
follow
horses
Inject creates array of empty strings and inside main block each string chars are prepended to proper array element

Case-insensitive matching in LPeg.re (Lua)

I'm new to the "LPeg" and "re" modules of Lua, currently I want to write a pattern based on following rules:
Match the string that starts with "gv_$/gv$/v$/v_$/x$/xv$/dba_/all_/cdb_", and the prefix "SYS.%s*" or "PUBLIC.%s*" is optional
The string should not follow a alphanumeric, i.e., the pattern would not match "XSYS.DBA_OBJECTS" because it follows "X"
The pattern is case-insensitive
For example, below strings should match the pattern:
,sys.dba_objects, --should return "sys.dba_objects"
SyS.Dba_OBJECTS
cdb_objects
dba_hist_snapshot) --should return "dba_hist_snapshot"
Currently my pattern is below which can only match non-alphanumeric+string in upper case :
p=re.compile[[
pattern <- %W {owner* name}
owner <- 'SYS.'/ 'PUBLIC.'
name <- {prefix %a%a (%w/"_"/"$"/"#")+}
prefix <- "GV_$"/"GV$"/"V_$"/"V$"/"DBA_"/"ALL_"/"CDB_"
]]
print(p:match(",SYS.DBA_OBJECTS"))
My questions are:
How to achieve the case-insensitive matching? There are some topics about the solution but I'm too new to understand
How to exactly return the matched string only, instead of also have to plus %W? Something like "(?=...)" in Java
Highly appreciated if you can provide the pattern or related function.
You can try to tweak this grammar
local re = require're'
local p = re.compile[[
pattern <- ((s? { <name> }) / s / .)* !.
name <- (<owner> s? '.' s?)? <prefix> <ident>
owner <- (S Y S) / (P U B L I C)
prefix <- (G V '_'? '$') / (V '_'? '$') / (D B A '_') / (C D B '_')
ident <- [_$#%w]+
s <- (<comment> / %s)+
comment <- '--' (!%nl .)*
A <- [aA]
B <- [bB]
C <- [cC]
D <- [dD]
G <- [gG]
I <- [iI]
L <- [lL]
P <- [pP]
S <- [sS]
U <- [uU]
V <- [vV]
Y <- [yY]
]]
local m = { p:match[[
,sys.dba_objects, --should return "sys.dba_objects"
SyS.Dba_OBJECTS
cdb_objects
dba_hist_snapshot) --should return "dba_hist_snapshot"
]] }
print(unpack(m))
. . . prints match table m:
sys.dba_objects SyS.Dba_OBJECTS cdb_objects dba_hist_snapshot
Note that case-insensitivity is quite hard to achieve out of the lexer so each letter has to get a separate rule -- you'll need more of these eventually.
This grammar is taking care of the comments in your sample and skips them along with whitespace so matches after "should return" are not present in output.
You can fiddle with prefix and ident rules to specify additional prefixes and allowed characters in object names.
Note: !. means end-of-file. !%nl means "not end-of-line". ! p and & p are constructing non-consuming patterns i.e. current input pointer is not incremented on match (input is only tested).
Note 2: print-ing with unpack is a gross hack.
Note 3: Here is a tracable LPeg re that can be used to debug grammars. Pass true for 3-rd param of re.compile to get execution trace with test/match/skip action on each rule and position visited.
Finally I got an solution but not so graceful, which is to add an additional parameter case_insensitive into re.compile, re.find, re.match and re.gsubfunctions. When the parameter value is true, then invoke case_insensitive_pattern to rewrite the pattern:
...
local fmt="[%s%s]"
local function case_insensitive_pattern(quote,pattern)
-- find an optional '%' (group 1) followed by any character (group 2)
local stack={}
local is_letter=nil
local p = pattern:gsub("(%%?)(.)",
function(percent, letter)
if percent ~= "" or not letter:match("%a") then
-- if the '%' matched, or `letter` is not a letter, return "as is"
if is_letter==false then
stack[#stack]=stack[#stack]..percent .. letter
else
stack[#stack+1]=percent .. letter
is_letter=false
end
else
if is_letter==false then
stack[#stack]=quote..stack[#stack]..quote
is_letter=true
end
-- else, return a case-insensitive character class of the matched letter
stack[#stack+1]=fmt:format(letter:lower(), letter:upper())
end
return ""
end)
if is_letter==false then
stack[#stack]=quote..stack[#stack]..quote
end
if #stack<2 then return stack[1] or (quote..pattern..quote) end
return '('..table.concat(stack,' ')..')'
end
local function compile (p, defs, case_insensitive)
if mm.type(p) == "pattern" then return p end -- already compiled
if case_insensitive==true then
p=p:gsub([[(['"'])([^\n]-)(%1)]],case_insensitive_pattern):gsub("%(%s*%((.-)%)%s*%)","(%1)")
end
local cp = pattern:match(p, 1, defs)
if not cp then error("incorrect pattern", 3) end
return cp
end
...

Can I create a gmatch pattern that returns a variadic number of values?

I need to iterate over some pairs of strings in a program that I am writing. Instead of putting the string pairs in a big table-of-tables, I am putting them all in a single string, because I think the end result is easier to read:
function two_column_data(data)
return data:gmatch('%s*([^%s]+)%s+([^%s]+)%s*\n')
end
for a, b in two_column_data [[
Hello world
Olá hugomg
]] do
print( a .. ", " .. b .. "!")
end
The output is what you would expect:
Hello, world!
Olá, hugomg!
However, as the name indicates, the two_column_data function only works if there are two exactly columns of data. How can I make it so it works on any number of columns?
for x in any_column_data [[
qwe
asd
]] do
print(x)
end
for x,y,z in any_column_data [[
qwe rty uio
asd dfg hjk
]] do
print(x,y,z)
end
I'm OK with using lpeg for this task if its necessary.
function any_column_data(data)
local f = data:gmatch'%S[^\r\n]+'
return
function()
local line = f()
if line then
local row, ctr = line:gsub('%s*(%S+)','%1 ')
return row:match(('(.-) '):rep(ctr))
end
end
end
Here is an lpeg re version
function re_column_data(subj)
local t, i = re.compile([[
record <- {| ({| [ %t]* field ([ %t]+ field)* |} (%nl / !.))* |}
field <- escaped / nonescaped
nonescaped <- { [^ %t"%nl]+ }
escaped <- '"' {~ ([^"] / '""' -> '"')* ~} '"']], { t = '\t' }):match(subj)
return function()
local ret
i, ret = next(t, i)
if i then
return unpack(ret)
end
end
end
It basicly is a redo of the CSV sample and supports quoted fields for some nice use-cases: values with spaces, empty values (""), multi-line values, etc.
for a, b, c in re_column_data([[
Hello world "test
test"
Olá "hug omg"
""]].."\tempty a") do
print( a .. ", " .. b .. "! " .. (c or ''))
end
local function any_column_data( str )
local pos = 0
return function()
local _, to, line = str:find("([^\n]+)\n", pos)
if line then
pos = to
local words = {}
line:gsub("[^%s]+", function( word )
table.insert(words, word)
end)
return table.unpack(words)
end
end
end
Outer loop returns lines, and inner loop returns words in line.
s = [[
qwe rty uio
asd dfg hjk
]]
for s in s:gmatch('(.-)\n') do
for s in s:gmatch('%w+') do
io.write(s,' ')
end
io.write('\n')
end

Formatting a table in Lua with string.gsub

The following code formats a table into a printable string, but I feel like this can be done a lot easier.
function printFormat(table)
local str = ""
for key, value in pairs(table) do
if value == 1 then
str = str .. string.gsub(value, 1, "A, ") -- Replaces 1 with A
elseif value == 2 then
str = str .. string.gsub(value, 2, "B, ") -- Replaces 2 with B
elseif value == 3 then
str = str .. string.gsub(value, 3, "C, ") -- Replaces 3 with C
elseif value == 4 then
str = str .. string.gsub(value, 4, "D, ") -- Replaces 4 with D
end
end
str = string.sub(str, 1, #str - 2) -- Removes useless chars at the end (last comma and last whitespace)
str = "<font color=\"#FFFFFF\">" .. str .. "</font>" -- colors the string
print(str)
end
local myTable = {1,4,3,2,3,2,1,3,4,2,2,...}
printFormat(myTable)
Is there a way to use a oneliner instead of having to loop through every index and make changes?
Or make the code more compact?
You can use a helper table to replace the multiple if statement:
local chars = {"A", "B", "C", "D"}
for _, v in ipairs(t) do
str = str .. chars[v] .. ", "
end
Or, if there are more than 1 to 4, try this:
for _, v in ipairs(t) do
str = str .. string.char(string.byte('A') + v) .. ", "
end
Use table.concat.
string.gsub can perform replacement using a replacement table.
Don't use names like table for your own variables.
Therefore:
function printFormat( tInput )
local sReturn = table.concat( tInput, ', ' )
sReturn = sReturn:gsub( '%d', {['1'] = 'A', ...} ) -- update table accordingly
return '<font color="#FFFFFF">' .. str .. "</font>"
end
and, for one liner:
return '<font color="#FFFFFF">' .. ( table.concat(tInput, ', ') ):gsub( '%d', {['1'] = 'A', ...} )

Resources