# How to line break a very long 1-row string array?

**URL:** <https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 24, 2021, 11:16am UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287 "2021-10-24T11:16:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 24, 2021, 11:16am UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/1 "2021-10-24T11:16:45Z")

</div>

I have a unit row like the below. You see “” because some of the units are none. For example, there is no unit for Year.  
`row2 = ["" "" "" "umol/kg" "" "" "kg/m3" "decimal degrees"];`  
In reality it is much longer, so I would need to break them into different lines.

Why does the below give me an error? It seems that instead of considering it as a 1-row array, Julia is trying to do a vcat for the elements in these two lines?

```julia
row2 = ["" "" "" "umol/kg" "" "" 
    "kg/m3" "decimal degrees"];

```

Error message:

```julia
ArgumentError: number of columns of each array must match (got (6, 2))

Stacktrace:
 [1] _typed_vcat(#unused#::Type{String}, A::Tuple{Matrix{String}, Matrix{String}})
   @ Base ./abstractarray.jl:1553
 [2] typed_vcat(::Type{String}, ::Matrix{String}, ::Matrix{String})
   @ Base ./abstractarray.jl:1567
 [3] typed_hvcat(::Type{String}, ::Tuple{Int64, Int64}, ::String, ::Vararg{String, N} where N)
   @ Base ./abstractarray.jl:1957
 [4] hvcat(::Tuple{Int64, Int64}, ::String, ::Vararg{String, N} where N)
   @ Base ./abstractarray.jl:1932
 [5] top-level scope
   @ In[330]:1
 [6] eval
   @ ./boot.jl:360 [inlined]
 [7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1116

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 24, 2021, 11:22am UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/2 "2021-10-24T11:22:05Z")

</div>

```julia
julia> [1 2 3
        4 5 6]
2×3 Matrix{Int64}:
 1 2 3
 4 5 6

julia> row2 = ["", "", "", "umol/kg", "", "", 
           "kg/m3", "decimal degrees"]
8-element Vector{String}:
 ""
 ""
 ""
 "umol/kg"
 ""
 ""
 "kg/m3"
 "decimal degrees"

```

read: [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#man-array-literals)

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 24, 2021, 11:32am UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/3 "2021-10-24T11:32:13Z")

</div>

Thank you for the reply!

I’m able to combine this with the below code to make it work:  
`row2 = reshape(row2, (1,8));`

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 24, 2021, 11:40am UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/4 "2021-10-24T11:40:24Z")

</div>

you can also hack it as

```julia
julia> row2 = ["" "" "" "umol/kg" "" "\
           " "kg/m3" "decimal degrees"]
1×8 Matrix{String}:
 "" "" "" "umol/kg" "" "" "kg/m3" "decimal degrees"

```

by leaving a `"` un-closed, parser will continue to next line, the `\` cancels the line-breaking `\n` that was suppose to be there.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 24, 2021, 12:45pm UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/5 "2021-10-24T12:45:57Z")

</div>

In Julia 1.7 there will also be the option

```julia
julia> row2 = ["" "" "" "umol/kg" "" "";;
               "kg/m3" "decimal degrees"]
1×8 Matrix{String}:
 "" "" "" "umol/kg" "" "" "kg/m3" "decimal degrees"

```

but I would recommend letting go of Matlab habits and just use a 1D vector.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 24, 2021, 12:58pm UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/6 "2021-10-24T12:58:49Z")

</div>

Many thanks! How do I use a 1D vector in this case?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 24, 2021, 1:12pm UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/7 "2021-10-24T13:12:31Z")

</div>

You construct it as shown earlier,

```julia
julia> vec2 = ["", "", "", "umol/kg", "", "", 
               "kg/m3", "decimal degrees"]
8-element Vector{String}:
 ""
 ""
 ""
 "umol/kg"
 ""
 ""
 "kg/m3"
 "decimal degrees"

```

Using it is up to the rest of your code. It depends on what you do more exactly with it, but usually a 1D vector of strings should be at least as easy to work with as a 1×N matrix in Julia.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 24, 2021, 1:58pm UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/8 "2021-10-24T13:58:39Z")

</div>

> [@leon](#):
>
> I’m able to combine this with the below code to make it work:  
> `row2 = reshape(row2, (1,8));`

You can ([equivalently](https://github.com/JuliaLang/julia/blob/dad40711c1bc27b24b01d9ac883929d145149b99/base/permuteddimsarray.jl#L191)) just use `permutedims(row2)`

Another alternative is to use a multi-line comment to absorb the line break:

```julia
row2 = ["" "" "" "umol/kg" "" "" #=
=# "kg/m3" "decimal degrees"]

```

`#= ... =#` can generally be used for line continuation in Julia.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 24, 2021, 4:02pm UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/9 "2021-10-24T16:02:05Z")

</div>

It’s great to learn this below trick! Many thanks for sharing!

`#= ... =#` can generally be used for line continuation in Julia.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 24, 2021, 4:04pm UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/10 "2021-10-24T16:04:31Z")

</div>

This is useful, but I still wouldn’t mind a line continuation syntax which doesn’t need something on both lines. Not a big deal though.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 24, 2021, 5:08pm UTC](https://discourse.julialang.org/t/how-to-line-break-a-very-long-1-row-string-array/70287/11 "2021-10-24T17:08:18Z")

</div>

I agree. Julia is designed to not use line breakers, but from time to time, my program would throw out an error because of breaking a line of code into multiple lines. They can usually be fixed by simply putting everything into one long line.
