Why would you use
text_result as an input? Strings are immutable, so you cannot change them. The scoping issue seems to me a red herring.
for rule in 1:5
if i % 2 == 1 # what is i supposed to be here??
text_result = replace(text, "a" => "b", count=1) #this is another string
This code just takes
text and replaces the first ‘a’ with ‘b’, over and over. You can replace your code with this function:
textrep(text) = replace(text, "a"=>"b", count=1)
Actually, you should use characters instead of strings in the replace call, since it is faster:
textrep(text) = replace(text, 'a'=>'b', count=1)
In summary: There is no point in pre-allocating
text_result because it just gets thrown away each time you assign the output from
replace to it.