Femto Trader
Femto Trader

Reputation: 2014

Concatenate 2 Julia Arrays without modifying them

I would like to concatenate 2 arrays.

julia> l1=["a","b"]
2-element Array{ASCIIString,1}:
 "a"
 "b"

julia> l2=["c","d"]
2-element Array{ASCIIString,1}:
 "c"
 "d"

append! can do this but this function is modifying l1 (that's a function named with a !)

julia> append!(l1, l2)
4-element Array{ASCIIString,1}:
 "a"
 "b"
 "c"
 "d"

julia> l1
4-element Array{ASCIIString,1}:
 "a"
 "b"
 "c"
 "d"

I was looking for a append function (without exclamation point).

But such a function doesn't seems to exist.

Any idea ?

Upvotes: 6

Views: 3966

Answers (2)

niczky12
niczky12

Reputation: 5063

In addition to @oleeinar's answer, you can use hcat and vcat to concatenate arrays:

l3 = vcat(l1, l2)
4-element Array{ASCIIString,1}:
 "a"
 "b"
 "c"
 "d"

You can also concatenate horizontally with hcat:

l4 = hcat(l1, l2)
2x2 Array{ASCIIString,2}:
 "a"  "c"
 "b"  "d"

Upvotes: 13

OleEinar
OleEinar

Reputation: 91

you can 'join' the arrays by

l3 = [l1; l2]

Upvotes: 9

Related Questions