# Better syntax for collecting into array including both end points

**URL:** <https://discourse.julialang.org/t/better-syntax-for-collecting-into-array-including-both-end-points/33246>\
**Category:** General Usage\
**Created:** [January 11, 2020, 7:22pm UTC](https://discourse.julialang.org/t/better-syntax-for-collecting-into-array-including-both-end-points/33246 "2020-01-11T19:22:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)\
**Post date:** [January 11, 2020, 7:22pm UTC](https://discourse.julialang.org/t/better-syntax-for-collecting-into-array-including-both-end-points/33246/1 "2020-01-11T19:22:24Z")

</div>

The following code shows two ways of collecting data into an array.  
MyArray0 may not include YrEnd depending on YrStep.  
MyArray1 always includes YrEnd regardless of YrStep.

Question, is there a better or more elegant way to code MyArray1…Archie

```julia
# sample data to drive MyArray0 and MyArray1
YrBeg = 1751
YrStep = 10
YrEnd = 1776
MyArray0 = collect(YrBeg:YrStep:YrEnd) # YrEnd may not be in MyArray0
MyArray1 = push!(collect(YrBeg:YrStep:YrEnd-1), YrEnd) # YrEnd always in MyArray1
@show(MyArray0, MyArray1)
# is there a simpler or more elegant way to code MyArray1?

```

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [January 11, 2020, 7:26pm UTC](https://discourse.julialang.org/t/better-syntax-for-collecting-into-array-including-both-end-points/33246/2 "2020-01-11T19:26:48Z")

</div>

You can write `vcat(YrBeg:YrStep:YrEnd-1, YrEnd)`, perhaps?

---

<div class="post-metadata">

**Author:** ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)\
**Post date:** [January 11, 2020, 7:35pm UTC](https://discourse.julialang.org/t/better-syntax-for-collecting-into-array-including-both-end-points/33246/3 "2020-01-11T19:35:05Z")

</div>

Yes. That is more concise.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 11, 2020, 9:23pm UTC](https://discourse.julialang.org/t/better-syntax-for-collecting-into-array-including-both-end-points/33246/4 "2020-01-11T21:23:10Z")

</div>

range(a, b, length=n)

---

<div class="post-metadata">

**Author:** ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)\
**Post date:** [January 11, 2020, 10:23pm UTC](https://discourse.julialang.org/t/better-syntax-for-collecting-into-array-including-both-end-points/33246/5 "2020-01-11T22:23:09Z")

</div>

That works if reals are allowed, however I desire all the values to be integers. Thx…Archie

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 11, 2020, 10:42pm UTC](https://discourse.julialang.org/t/better-syntax-for-collecting-into-array-including-both-end-points/33246/6 "2020-01-11T22:42:58Z")

</div>

OK I see, you didn’t actually say that as far as I can see.

Here’s an alternative syntax for the `vcat` suggestion:

```julia
julia> n = 5
5

julia> [1:2:n-1; n]
3-element Array{Int64,1}:
 1
 3
 5

```

---

<div class="post-metadata">

**Author:** ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)\
**Post date:** [January 12, 2020, 2:27am UTC](https://discourse.julialang.org/t/better-syntax-for-collecting-into-array-including-both-end-points/33246/7 "2020-01-12T02:27:58Z")

</div>

Good bust! Shortest yet.
