1

I have trouble on my test script

names.txt contents
foo 1
test 0

data="names.txt"
name="test"
--
d=io.open(data,"r")
s=d:read("*a")
f=string.gsub(s,"%"..name,"%1 1")
print(f)
print"------"
print(f:gsub("(%w+)%s*(%d)","%1"):format("%s"))

output on lua

foo 1
test 1 0
------
foo
test 0

I would like to get the first number with string

from test 0 to test 1

I hope someone can help me

hjpotter92
  • 78,589
  • 36
  • 144
  • 183

1 Answers1

2

Is that what you are trying to do?

data="names.txt"
name="test"
--
d=io.open(data,"r")
s=d:read("*a")
f=string.gsub(s,"(" .. name .. ")%s+%d+","%1 1")
print(f)

Result:

foo 1
test 1

If not, please be more precise. What output do you want?

catwell
  • 6,770
  • 1
  • 23
  • 21
  • thanks that Is was I trying to do. thank you very much – Yo Crus Salazar Oct 22 '13 at 09:34
  • 1
    keep in mind that this pattern breaks if `name` contains any pattern control chars like `%`, `.`, `-`, `(`, etc. You should prepare the name first by properly escaping those control chars -- or use a more generic pattern (like `(%w+)%s+%d+`) followed by simple string comparison (`if capture == name then...`). – dualed Oct 22 '13 at 13:43