# Print.(v) for vector of custom structure?

**URL:** <https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699>\
**Category:** General Usage\
**Created:** [October 13, 2022, 11:07pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699 "2022-10-13T23:07:02Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 13, 2022, 11:07pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/1 "2022-10-13T23:07:02Z")

</div>

What is going on here?

```julia
julia> struct A
           x::Int
       end

julia> Base.show(io::IO, a::A) = print(io, "$(a.x)")

julia> v = [A(1), A(2)];

julia> print.(v)
122-element Vector{Nothing}:
 nothing
 nothing

```

this is a fresh Julia section.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 13, 2022, 11:18pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/2 "2022-10-13T23:18:13Z")

</div>

Or even simpler:

```julia
julia> struct A
           x::Int
       end

julia> v = [A(1), A(2)]
2-element Vector{A}:
 A(1)
 A(2)

julia> println.(v)
A(1)
A(2)
2-element Vector{Nothing}:
 nothing
 nothing

```

Did this always happened?

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [October 13, 2022, 11:20pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/3 "2022-10-13T23:20:04Z")

</div>

`println` returns `nothing`; broadcasting collects the two `nothing`s in an array.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 14, 2022, 12:13am UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/4 "2022-10-14T00:13:29Z")

</div>

> [@lmiq](#):
>
> ```julia
> julia> Base.show(io::IO, a::A) = print(io, "$(a.x)")
> 
> julia> v = [A(1), A(2)];
> 
> julia> print.(v)
> 122-element Vector{Nothing}:
> 
> ```

you printed `1` then `2` then Julia REPL shows you the `[nothing, nothing]` as

```julia
2-element Vector{Nothing}:
 nothing
 nothing

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 14, 2022, 12:20am UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/6 "2022-10-14T00:20:50Z")

</div>

If you want an array of strings, then you need to call a function that _returns_ a string, e.g.

```julia
julia> repr.(v)
2-element Vector{String}:
 "1"
 "2"

```

Or if you _just_ want the `println` output, then put a semicolon after the call to suppress the display of the `[nothing, nothing]` return value:

```julia
julia> println.(v);
1
2

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 14, 2022, 12:39am UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/7 "2022-10-14T00:39:45Z")

</div>

Thanks for the clarifications, to all.

I’m surprised I didn’t notice this before. This came in the following context. I have a vector of custom structs, but now I notice this is irrelevant, because even with simple arrays it is the same:

If I have an array, when it is displayed, it is adjusted to the screen height:

```julia
julia> x
1000-element Vector{Float64}:
 0.379186956550142
 0.29159820914033496
 ⋮
 0.20866068645567148
 0.2915546149517577

julia>

```

Now I want to see all the elements (by scrolling the screen, of course). So why not:

```julia
julia> println.(x)

```

And this does print everything, but followed by the array of `nothing`s.

So, what is the pleasant way to just display all the elements of an array, in one line? Adding the `;` afterwards, as suggested by @stevengj, or is there another function that people use that does not collect that array of nothings at the end?

(by the way, any `more`, `less`, type of functions for arrays implemented somewhere?)

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [October 14, 2022, 12:59am UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/8 "2022-10-14T00:59:32Z")

</div>

> [@lmiq](#):
>
> what is the pleasant way to just display all the elements of an array, in one line?

How about

```julia
julia> printall(x) = for i in x print(i, " "); end

julia> x = rand(10);

julia> printall(x)
0.9225340578326212 0.07352896862906577 0.6508288404547642 0.23723598173792448 0.46214011307549796 0.12091754019493772 0.8345915278753898 0.025769195692800673 0.2621634432952713 0.06305239378926508

```

Use `println(i)` instead of `print(i, " ")` if you want one element per line.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 14, 2022, 1:02am UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/9 "2022-10-14T01:02:07Z")

</div>

Oh, no, I just want to know the most convenient way to do it on the REPL eventually. `println.(x);` is fine. Better yet if some package provides sort of `more` and `less` functions that allow some interactivity in the array display.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [October 14, 2022, 1:23am UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/10 "2022-10-14T01:23:20Z")

</div>

```julia
julia> x = [1, 2, 3];

julia> foreach(println, x)
1
2
3

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 14, 2022, 1:47am UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/11 "2022-10-14T01:47:40Z")

</div>

> [@lmiq](#):
>
> `println.(x);`

that’s what I do and yes I hate how many times I have to do it every day

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 14, 2022, 2:06am UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/12 "2022-10-14T02:06:27Z")

</div>

> [@lmiq](#):
>
> So, what is the pleasant way to just display all the elements of an array, in one line?

`show(stdout, "text/plain", x)` works. Or you can do `println(x)`, though that shows them horizontally.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 14, 2022, 2:16am UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/13 "2022-10-14T02:16:25Z")

</div>

Maybe [this](https://github.com/ronisbr/TerminalPager.jl) by @Ronis_BR?

If you do

```julia
julia> using TerminalPager
julia> rand(5000) |> pager

```

You will get this screen, in which you can scroll up and down, much a-la less!

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/3/a395f54e9117b0af8c9d55b6473f4ab1fedb29b0.png)

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [October 14, 2022, 5:41pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/14 "2022-10-14T17:41:08Z")

</div>

> [@aramirezreyes](#):
>
> You will get this screen, in which you can scroll up and down, much a-la less!

Off-topic: TerminalPager.jl was once called Less.jl 😃

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 14, 2022, 5:41pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/15 "2022-10-14T17:41:55Z")

</div>

Ah! So I am not crazy! I was looking for it with that name!

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [October 14, 2022, 5:42pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/16 "2022-10-14T17:42:54Z")

</div>

We decided to rename it because `less` is a “joke” with `more` and it is more restricted to \*nix uses.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [October 14, 2022, 5:45pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/17 "2022-10-14T17:45:44Z")

</div>

> [@jling](#):
>
> that’s what I do and yes I hate how many times I have to do it every day

Using TerminalPager.jl, you can type `|` to enter in pager mode. In this case, all output that is larger than the screen will be rendered inside the pager.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 14, 2022, 5:49pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/18 "2022-10-14T17:49:24Z")

</div>

sadly I often needs this from Juputer

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [October 14, 2022, 6:51pm UTC](https://discourse.julialang.org/t/print-v-for-vector-of-custom-structure/88699/19 "2022-10-14T18:51:41Z")

</div>

Maybe is time for a HtmlPager 🙂
