# Having trouble appending a string to an array using push!

**URL:** <https://discourse.julialang.org/t/having-trouble-appending-a-string-to-an-array-using-push/16281>\
**Category:** New to Julia\
**Created:** [October 13, 2018, 5:37pm UTC](https://discourse.julialang.org/t/having-trouble-appending-a-string-to-an-array-using-push/16281 "2018-10-13T17:37:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [October 13, 2018, 5:37pm UTC](https://discourse.julialang.org/t/having-trouble-appending-a-string-to-an-array-using-push/16281/1 "2018-10-13T17:37:33Z")

</div>

Hello guys,  
I am struggling with how to get push! to add my nextpitch variable to my pitcharray array. I get this error message: ERROR: MethodError: no method matching push!(::String, ::String)

Thanks,  
Nakul

```julia
pitch0 = 200
    pitcharray = "d pitch1 $pitch0;"
    pitchn = 5
    pitchn = 1:pitchn
    multiplier = 4
    for n = 1:size(pitchn,1)
        nextpitch = "d pitch$n $(pitch0*multiplier*pitchn[n])"
        push!(pitcharray,nextpitch)
    end

```

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [October 13, 2018, 5:54pm UTC](https://discourse.julialang.org/t/having-trouble-appending-a-string-to-an-array-using-push/16281/2 "2018-10-13T17:54:02Z")

</div>

I have troubles understanding what you are trying to do but `pitcharray` is not an array; it’s a string.

You can do something like `pitcharray = ["d pitch1 $pitch0"]` but as I said, don’t know what you want to do 😉

If you want to convert the array to a string again, you can do it for example like this:

```julia
final_pitch = foldr(*, pitcharray)
println(final_pitch)

```

Alternatively (just noticed you are using the `;` as a delimiter):

```julia
final_pitch = join(pitcharray, ';')
println(final_pitch)

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [October 13, 2018, 7:51pm UTC](https://discourse.julialang.org/t/having-trouble-appending-a-string-to-an-array-using-push/16281/3 "2018-10-13T19:51:10Z")

</div>

I think you might want to do something like this, but I’m not 100% sure what you are trying to accomplish

```nohighlight
pitcharray *= "d pitch$n $(pitch0*multiplier*pitchn[n])"

```

`String` can be concatenated with the `*` operator, it’s not an `Array` by the way.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [October 13, 2018, 9:25pm UTC](https://discourse.julialang.org/t/having-trouble-appending-a-string-to-an-array-using-push/16281/4 "2018-10-13T21:25:08Z")

</div>

If it’s an array of strings you want, I’d recommend something like this:

```julia
[n => "d pitch$n $(pitch0*multiplier*n)" for n = 1:5]

```

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [October 13, 2018, 9:56pm UTC](https://discourse.julialang.org/t/having-trouble-appending-a-string-to-an-array-using-push/16281/5 "2018-10-13T21:56:15Z")

</div>

Thank you guys. I like all the methods. My understanding is that @bennedich’s solution is the most compact. @chakravala’s solution keeps everything as a string instead of an array. @tamasgal’s solution was the easiest for me to implement because it just involved me bracketing the pitcharray to make it into an array (I had thought a string is automatically an array, but I guess it’s of type string)! I also like the foldr function, I’d never heard of it!

Thanks for the help!
