atbug
atbug

Reputation: 838

How can I initiate a list with a list element in julia?

I want to make a list of list in julia. It should be like this:

a = [1,"char", [1, 2, 3]]
a[3]
# ouput should be [1,2,3]

However, julia automatically does concatenation, so it ends up to be [1, "char", 1, 2, 3]

How can I do such things in julia without initiating the list with another value and then assigning a list to it like:

a = [1, "char", 3]
a[3] = [1, 2, 3]

Upvotes: 3

Views: 175

Answers (1)

Reza Afzalan
Reza Afzalan

Reputation: 5746

julia> a = Any[1,"char", [1, 2, 3]]
3-element Array{Any,1}:
 1
  "char"
  [1,2,3]

The style of array concatenation have been changes, now to concat arrays, the right syntax is: a = [1;"char"; [1, 2, 3]], so [1;"char"; [1, 2, 3]]==Any[1;"char"; [1, 2, 3]] # => true but when Julia faces [1,"char", [1, 2, 3]] by default it tries to concat possible element types.

Upvotes: 6

Related Questions