# How to change string in local scope

**URL:** https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572
**Category:** New to Julia
**Tags:** strings
**Created:** [September 9, 2019, 6:23pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572 "2019-09-09T18:23:09Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![sergeypospelov](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@sergeypospelov](https://discourse.julialang.org/u/sergeypospelov)
#### Post date: [September 9, 2019, 6:23pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/1 "2019-09-09T18:23:09Z")

</div>

Hello, guys! I am new to Julia. Help me please. I want to change my string initialized in upper local scope, and then use it there

```julia
text = "abacaba"
text_result = "" #string must be changed

for rule in 1:5
    if i % 2 == 1 #some condition
        text_result = replace(text, "a" => "b", count=1) #this is another string
    end
end
print(text_result) # empty string will be printed

```

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [September 9, 2019, 6:28pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/2 "2019-09-09T18:28:35Z")

</div>

To re-assign the global variable `text_result` you need `global` in the loop.

I highly recommend creating a function around this loop with `text_result` as an input (in which case you do not need `global`).

---

<div class="post-metadata">

### Author: ![pmarg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmarg/32/936_2.png) [@pmarg](https://discourse.julialang.org/u/pmarg)
#### Post date: [September 9, 2019, 6:29pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/3 "2019-09-09T18:29:19Z")

</div>

If the loop is not inside a function you need to declare `test_results` as a global AFAIK:

```julia
text = "abacaba"
text_result = "" #string must be changed

for rule in 1:5
    if i % 2 == 1 #some condition
       global text_result = replace(text, "a" => "b", count=1) #this is another string
    end
end
print(text_result) # not empty string will be printed

```

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [September 9, 2019, 6:40pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/4 "2019-09-09T18:40:33Z")

</div>

> [@sergeypospelov](#):
>
> Hello, guys! I am new to Julia.

The best advice is to use Jupyter, which does not have this issue. The code will work the way that is intuitive. Otherwise, put any `for` loops in a function when working in the REPL or a `.jl` file.

If you follow those rules and ever have to write `global`, then (as a beginner) you are probably are doing something you shouldn’t

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 9, 2019, 6:59pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/5 "2019-09-09T18:59:54Z")

</div>

> [@jlperla](#):
>
> The best advice is to use Jupyter, which does not have this issue.

Nah, this is not “the best advice”. The best advice is exactly what happened here, someone learned about what a global variable is and how to change it’s binding and then went on coding exactly as before. There is absolutely no need to completely change your coding environment because of this.

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [September 9, 2019, 7:10pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/6 "2019-09-09T19:10:52Z")

</div>

> [@kristoffer.carlsson](#):
>
> The best advice is exactly what happened here,

The best advice is to make a new user learn about a complicated and frequently hated scoping rule that is only beloved by people who commit to core compiler? Furthermore, one which is completely alien to _every_ other language the user may come from?

Or, alternatively, get started in a the beginner friendly environment and hope people eventually fix this problem - as has been discussed for a year.

I stick with my advice: Use jupyter, and wrap things in functions when you are not in jupyter.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [September 9, 2019, 7:17pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/7 "2019-09-09T19:17:15Z")

</div>

> [@jlperla](#):
>
> The best advice is to use Jupyter, which does not have this issue.

Sorry if we seem to be jumping all over you here, but bear in mind, there are some of us who absolutely detest Jupyter, I don’t think it’s a great idea to give people the impression that there is somehow something wrong with Julia outside of Jupyter. Of course, if someone wanted to use it anyway, it’s great to present it as an option.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 9, 2019, 7:21pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/8 "2019-09-09T19:21:09Z")

</div>

> [@jlperla](#):
>
> The best advice is to make a new user learn about a complicated and frequently hated scoping rule that is only beloved by people who commit to core compiler?

Complicated? It took one sentence and the reply was marked as a solution. Anyway, I am not going to pollute this thread further, I just didn’t want a bad advice stand uncontested.

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [September 9, 2019, 7:31pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/9 "2019-09-09T19:31:39Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [September 9, 2019, 8:58pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/10 "2019-09-09T20:58:33Z")

</div>

> [@ExpandingMan](#):
>
> I highly recommend creating a function around this loop with `text_result` as an input (in which case you do not need `global` ).

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.

```julia
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
    end
end

```

This code just takes `text` and replaces the first ‘a’ with ‘b’, over and over. You can replace your code with this function:

```julia
textrep(text) = replace(text, "a"=>"b", count=1)

```

Actually, you should use characters instead of strings in the replace call, since it is faster:

```julia
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.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [September 9, 2019, 10:09pm UTC](https://discourse.julialang.org/t/how-to-change-string-in-local-scope/28572/11 "2019-09-09T22:09:43Z")

</div>

> [@DNF](#):
>
> Why would you use `text_result` as an input?

Sorry, I meant `text`.
