Truly stupid newbie question re: loops and string.sub

To whoever takes pity on me by reading this post…

I’m not a programmer by trade, so having spent the last several hours trying to digest “Programming in Lua”, I’m significantly humbled enough to cry uncle and ask for help!

So… what I want to do is pretty simple. Take words in a string and replace them with other words. Fill in the blanks, as it were.

I can make it work, but it’s ugly. I know a do while or for loop will do the trick… just can’t seem to get they syntax right. Any help is most appreciated!!

Here’s what I have (side question: how does everyone else get their code pasted in so neatly and formatted?)

–Test to replace words in a string

local currentStory = “I want to insert noun one here and noun two here and noun three here”
print("Original story is this: " … currentStory)

local noun = {}
noun[1] = “club”
noun[2] = “cat”
noun[3] = “computer”

currentStory = string.gsub (currentStory, “noun”, noun[1], 1)
print("+++++")
print(currentStory)

currentStory = string.gsub (currentStory, “noun”, noun[2], 1)
print("+++++")
print(currentStory)

currentStory = string.gsub (currentStory, “noun”, noun[3], 1)
print("+++++")
print(currentStory)
–[[ tried to get this to work as a loop with no luck using the following code
for i = 1, 3 do
local x = 1
currentStory = string.gsub ( currentStory, “noun”, noun[x], 1)
print("Next the story is this: " … currentStory)
x = x + 1
end --]]

–Final text should read “I want to insert club one here and cat two here and computer three here”

Thanks so much!

Chris [import]uid: 97836 topic_id: 24970 reply_id: 324970[/import]

Hi.

The reason why it isn’t working is because you’re resetting the x variable to 1 every time it runs through your loop. You can resolve this by placing x=1 outside the loop or you can use the i variable instead like this:

for i = 1, 3 do  
currentStory = string.gsub ( currentStory, "noun", noun[i], 1)  
print("Next the story is this: " .. currentStory)  
end  

In addition to this you can place your print statement after the loop if you want the sentence to be printed only once.

And if you want to show your code this way, you use the < then write code and end it with a >. To mark the end of the code-segment you use the same tags but with an additional / after the less than sign. [import]uid: 129287 topic_id: 24970 reply_id: 101360[/import]

Dr. Mabuse,

Thank you so much! There must be some built in function in the for loop where i is incremented by one at each iteration?

Thanks for your help :slight_smile:

Chris [import]uid: 97836 topic_id: 24970 reply_id: 101368[/import]

No problem.

The for-loop will increment by 1 if you don’t tell it to do otherwise. Howerver, if you want to step through the loop with other increments you can use a third optional variabel called a step. So let’s say you want the loop to print every odd number between 1 and 10, you could use the following statement:

for i = 1, 10, 2 do

This will step through the loop with increments of 2

Or you could even tell it to count down from 10 to 1 by using this statement:

for i = 10, 1, -1 do

Hope this helps. [import]uid: 129287 topic_id: 24970 reply_id: 101394[/import]

It helps very much - thank you so much! [import]uid: 97836 topic_id: 24970 reply_id: 101435[/import]