Hi,
I’m having trouble with splice!
This works fine
d=[1,3]
splice!(d,2:1,2)
julia> d
3-element Vector{Int64}:
1
2
3
But this does not work…
d=[Date(“2021-01-01”),Date(“2021-03-01”)]
splice!(d,2:1,Date(“2021-02-01”))
fails with ERROR: MethodError: no method matching length(::Date)
splice!(d,2:1,) also doesn’t work
I can’t find any documentation referring to limitations of splice, can anyone point me in the right direction? Or perhaps give me an alternate approach for inserting Date elements into an array of Dates.
Windows 10
VScode 1.6.1
Julia Ext 1.4.3
Julia 1.6.3
Thanks
Steve
Try wrapping the spliced value in a vector, or use insert!
:
julia> splice!(d, 2:1, [Date("2021-02-01")])
Date[]
julia> d
3-element Vector{Date}:
2021-01-01
2021-02-01
2021-03-01
julia> insert!(d, 2, Date("2021-02-01"))
3-element Vector{Date}:
2021-01-01
2021-02-01
2021-03-01
Thanks stillyslalom, those worked perfectly. Julia continues to humble me but I love it.
FWIW, wrapping the Date in a tuple works too:
using Dates
d=[Date("2021-01-01"), Date("2021-03-01")]
splice!(d, 2:1, (Date("2021-02-01"),))
From the help doc:
If specified, replacement values from an ordered collection will be spliced in place of the removed items; in this case, indices must be a UnitRange.