How do I extract numbers from a string? - lua

How would I extract every number from a string and put them in an array?
For example the string:
"\113\115\106\111\117\41\40\105\102\109\109\112\40\42"

You can use string.gmatch like this:
local my_array = {}
local my_string = "\\113\\115\\106\\111\\117\\41\\40\\105\\102\\109\\109\\112\\40\\42"
print(my_string) --note how the string is \ followed by digits
for number in string.gmatch(my_string, "\\(%d+)") do
my_array[#my_array + 1] = tonumber(number)
print(number)
end
This will get you an table with all the numbers from your string.
The \ is escaped in my example to make it equal to the string you stated.
If i misunderstood your question and the numbers you want are from the chars then you need to do
local my_array = {}
local my_string = "\113\115\106\111\117\41\40\105\102\109\109\112\40\42"
print(my_string) --note how the string is letters
for char in string.gmatch(my_string, ".") do
my_array[#my_array + 1] = string.byte(char)
print(char, my_array[#my_array])
end

Related

how to get numbers from string in LUA?

I have string "0,0,0,-58.43083113,,"
and how could I get all the 4 numbers as double with LUA? Thanks!
I have tried with string.match(). But it didn't work.
local text = "0,0,0,-58.43083113,,"
local numbers = {}
text:gsub("[^,]+", function (str) table.insert(numbers, tonumber(str)+.0) end)
print(table.concat(numbers, ", "))
or
for str in text:gmatch("[^,]+") do
table.insert(numbers, tonumber(str) + .0)
end
Of course this assumes that you only have number representations and commas in your string.

Trying to make function which takes string as input and returns no. of words in whole string

**It takes Input as a string such as this - 'Nice one'
And Output gives - 4,3 (which is no. Of words in sentence or string)
**
function countx(str)
local count = {}
for i = 1, string.len(str) do
s = ''
while (i<=string.len(str) and string.sub(str, i, i) ~= ' ' ) do
s = s .. string.sub(str, i, i)
i = i+1
end
if (string.len(s)>0) then
table.insert(count,string.len(s))
end
end
return table.concat(count, ',')
end
You can find a simple alternative with your new requirements:
function CountWordLength (String)
local Results = { }
local Continue = true
local Position = 1
local SpacePosition
while Continue do
SpacePosition = string.find(String, " ", Position)
if SpacePosition then
Results[#Results + 1] = SpacePosition - Position
Position = SpacePosition + 1
-- if needed to print the string
-- local SubString = String:sub(Position, SpacePosition)
-- print(SubString)
else
Continue = false
end
end
Results[#Results + 1] = #String - Position + 1
return Results
end
Results = CountWordLength('I am a boy')
for Index, Value in ipairs(Results) do
print(Value)
end
Which gives the following results:
1
2
1
3
def countLenWords(s):
s=s.split(" ")
s=map(len,s)
s=map(str,s)
s=list(s)
return s
The above functions returns a list containing number of characters in each word
s=s.split(" ") splits string with delimiter " " (space)
s=map(len,s) maps the words into length of the words in int
s=map(str,s) maps the values into string
s=list(s) converts map object to list
Short version of above function (all in one line)
def countLenWords(s):
return list(map(str,map(len,s.split(" "))))
-- Localise for performance.
local insert = table.insert
local text = 'I am a poor boy straight. I do not need sympathy'
local function word_lengths (text)
local lengths = {}
for word in text:gmatch '[%l%u]+' do
insert (lengths, word:len())
end
return lengths
end
print ('{' .. table.concat (word_lengths (text), ', ') .. '}')
gmatch returns an iterator over matches of a pattern in a string.
[%l%u]+ is a Lua regular expression (see http://lua-users.org/wiki/PatternsTutorial) matching at least one lowercase or uppercase letter:
[] is a character class: a set of characters. It matches anything inside brackets, e.g. [ab] will match both a and b,
%l is any lowercase Latin letter,
%u is any uppercase Latin letter,
+ means one or more repeats.
Therefore, text:gmatch '[%l%u]+' will return an iterator that will produce words, consisting of Latin letters, one by one, until text is over. This iterator is used in generic for (see https://www.lua.org/pil/4.3.5.html); and on any iteration word will contain a full match of the regular expression.

how to remove a second AMPERSAND in a string?

1.how to remove second AMPERSAND from following string?
"apple&banana&grape&apple"
2.how to split the string at second AMPERSAND for following string? I want to ignore the first AMPERSAND and split from second AMPERSAND onwards.
"apple&banana&grape&apple"
arr = "apple&banana&grape&apple".split('&')
#arr = ["apple", "banana", "grape", "apple"]
To solve first query,
arr[0..1].join('&').concat(arr[2..-1].join('&'))
# "apple&bananagrape&apple"
For second query,
[words[0..1].join('&'), words[2..-1]].flatten
#["apple&banana", "grape", "apple"]
You can use gsub with a block:
For your first case:
index_to_remove = 2
current_index = 0
my_string = "apple&banana&grape&apple"
my_string.gsub('&') { |x| current_index += 1; current_index == index_to_remove ? '' : x}
#=> "apple&bananagrape&apple"
For your second case, you can replace the second & with a unique value and split on that:
index_to_split = 2
current_index = 0
my_string = "apple&banana&grape&apple"
my_string.gsub!('&') { |x| current_index += 1; current_index == index_to_split ? '&&' : x}
my_string.split('&&')
#=> ["apple&banana", "grape&apple"]
If you split the string by the &, it's quite simple to rejoin the first two / last two and convert back into a string or array:
words = "apple&banana&grape&apple".split('&')
# First query
words[0..1].join('&') + words[2..-1].join('&')
# Giving you: "apple&bananagrape&apple"
# Second query
[words[0..1].join('&'), words[2..-1].join('&')]
# Giving you: ["apple&banana", "grape&apple"]
You could tweak this for different length strings and separators as needed.
I imagine there's a good solution using regex matchers, but I'm not too hot on them, so hope this helps in some way!

Iterate Chinese string in Lua / Torch

I have a lua string in Chinese, such as
str = '这是一个中文字符串' -- in English: 'this is a Chinese string'
Now I would like to iterate the string above, to get the following result:
str[1] = '这'
str[2] = '是'
str[3] = '一'
str[4] = '个'
str[5] = '中'
str[6] = '文'
str[7] = '字'
str[8] = '符'
str[9] = '串'
and also output 9 for the length of the string.
Any ideas?
Something like this should work if you are using utf8 module from Lua 5.3 or luautf8, which works with LuaJIT:
local str = '这是一个中文字符串'
local tbl = {}
for p, c in utf8.codes(str) do
table.insert(tbl, utf8.char(c))
end
print(#tbl) -- prints 9
I haven't used non-english characters in lua before and my emulator just puts them in as '?' but something along the lines of this might work:
convert = function ( str )
local temp = {}
for c in str:gmatch('.') do
table.insert(temp, c)
end
return temp
end
This is a simple function that utilizes string.gmatch() to separate the string into individual characters and save them into a table. It would be used like this:
t = convert('abcd')
Which would make 't' a table containing a, b, c and d.
t[1] = a
t[2] = b
...
I am not sure if this will work for the Chinese characters but it is worth a shot.

How to split string by string length and a separator?

I'm trying to split a string into 2 strings when main string is over 30 chars and separator I wanted to use is a simple space between chars(the last space between words in main string) so it won't cut words. I'm asking you guys for help because I'm not very good with patterns in Lua.
local function split(str, max_line_length)
local lines = {}
local line
str:gsub('(%s*)(%S+)',
function(spc, word)
if not line or #line + #spc + #word > max_line_length then
table.insert(lines, line)
line = word
else
line = line..spc..word
end
end
)
table.insert(lines, line)
return lines
end
local main_string = 'This is very very very very very very long string'
for _, line in ipairs(split(main_string, 20)) do
print(line)
end
-- Output
This is very very
very very very very
long string
If you just want to split the string at the last space between words, try this
s="How to split string by string length and a separator"
a,b=s:match("(.+) (.+)")
print(s)
print(a)
print(b)

Resources