El domingo, 17 de mayo de 2015, 16:58:19 (UTC-5), Kuba Roth escribió:
>
> I see, so similarly this works with any level of nesting:
> julia> Array[[1,2],Array[[3],Array[[4],[6,7]]]]
> 2-element Array{Array{T,N},1}:
>  [1,2]                      
>  Array[[3],Array[[4],[6,7]]]
>
> Thanks
>
> On Sunday, May 17, 2015 at 12:59:28 PM UTC-7, Mauro wrote:
>>
>> This works: 
>>
>> julia> Array[[1,2],Array[[3],[4,5]]] 
>> 2-element Array{Array{T,N},1}: 
>>  [1,2]           
>>  Array[[3],[4,5]] 
>>
>
However, the type is incompletely specified (as given away by "Array{T,N}" 
where T and N are types that have not been defined.

An alternative, that seems to me neater, is to specify that the type stored 
in the array be Any -- literally, the elements of the array can be of any 
type:

Any[ [3], [4,5] ] 

Any[ [3], Any[ [4], [6,7] ] ]

This prints out different things on v0.3 and v0.4, but I believe it has the 
same result (and will not be affected
by the change to concatenation referred to by Mauro.

It's perhaps more readable to build it up one element at a time:

L = Any[ ]
push!(L, [3])
push!(L, Any[ [4], [6,7] ])

David.


 

>
>> (BTW, automatic concatenation is being changed in 0.4 & 0.5) 
>>
>> On Sun, 2015-05-17 at 21:41, Kuba Roth <[email protected]> wrote: 
>> > I've seen similar replies to my question on the forum but I still can't 
>> > figure out how to initialize nested arrays of a depth higher then 2? 
>> > 
>> > For instance this works fine: 
>> > julia> Array[[1,2],[3,4]] 
>> > 2-element Array{Array{T,N},1}: 
>> >  [1,2] 
>> >  [3,4] 
>> > 
>> > 
>> > But the following does not return what I expect. The [[3],[4,5]] part 
>> is 
>> > "flattened" into a single array. 
>> > julia> Array[[1,2],[[3],[4,5]]]   
>> > 2-element Array{Array{T,N},1}: 
>> >  [1,2]   
>> >  [3,4,5] 
>> > 
>> > 
>> > So far my workaround is to store the intermediate result and then 
>> assign it 
>> > to the final array. Can that be shortened to a single line? 
>> > b = Array[[3],[4,5]]   
>> > julia> Array[[1,2],b] 
>> > 2-element Array{Array{T,N},1}: 
>> >  [1,2]           
>> >  Array[[3],[4,5]] 
>> > 
>> > 
>> > Thank you. 
>>
>>

Reply via email to