[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: What's wrong with my string.gsub()?
- From: Steve Litt <slitt@...>
- Date: Sun, 2 Jan 2011 20:21:44 -0500
Hi all,
Here's a nonworking mock-up of code to implement Prompted Argument
Substitution in my UMENU program. In the string str, things of the form
%x%prompt%% should trigger a prompt for the user to type in information which
replaces the thing of the form %x%prompt%%. The UMENU specifications demand
that all prompts be presented to the user in sort order, not in the order they
appear in the command. So I first insert them all in the words array, then
sort the words array so they are presented in sorted order, then present them
as prompts so the user can type in the information, and on each, I do a
string.gsub() to replace that prompt with the user's response. Unfortunately
it doesn't work and I have no idea why.
=================================
str = "mycommand %c%Carg please=>%% %b%Barg please=>%% %a%Aarg please=>%%
final args"
words = {}
for w in string.gfind(str, "%%.%%.-%%%%") do
table.insert(words, w)
end
table.sort(words, function (a, b)
return string.lower(a) < string.lower(b)
end)
io.write("B4 , str=>"); print(str)
for k,v in ipairs(words, k) do
local temp = string.gsub(v, "%%.%%", "")
local response
local junk = str
temp = string.gsub(temp, "%%%%", "")
io.write(temp)
response = io.read()
print(v)
print(response)
str = string.gsub(str, v, response)
--str = string.gsub(str, tostring(v), response,1)
print(str)
end
io.write("Aft, str=>"); print(str)
=================================
Here's a session:
******************************************
slitt@mydesk:~/ulua/program$ ./test.lua
B4 , str=>mycommand %c%Carg please=>%% %b%Barg please=>%% %a%Aarg please=>%%
final args
Aarg please=>al
%a%Aarg please=>%%
al
mycommand %c%Carg please=>%% %b%Barg please=>%% %a%Aarg please=>%% final args
Barg please=>ben
%b%Barg please=>%%
ben
mycommand %c%Carg please=>%% %bben% %a%Aarg please=>%% final args
Carg please=>cim
%c%Carg please=>%%
cim
mycommand %c%Carg please=>%% %bben% %a%Aarg please=>%% final args
Aft, str=>mycommand %c%Carg please=>%% %bben% %a%Aarg please=>%% final args
slitt@mydesk:~/ulua/program$
******************************************
Everything works until the final string.gsub(), and that fails to substitute
on A and C, and substitutes in an odd manner on B. Anyone see what I'm doing
wrong?
Thanks
SteveT
Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt