# How to print some elements of array in one line in a file?

**URL:** https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895
**Category:** General Usage
**Tags:** io, formatting
**Created:** [August 24, 2021, 5:19am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895 "2021-08-24T05:19:37Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 24, 2021, 5:19am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/1 "2021-08-24T05:19:38Z")

</div>

I have a small question which is a little similar with not the same as

> [@Can I define string as "%Fmt" in Printf?](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/2):
>
> It seems to be the same problem with the same solution as in the other post: s = Printf.Format("%f %F %f %F"); Printf.format(s, Inf, Inf, NaN, NaN)

and

> [@How to repeat the format in @printf?](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/14):
>
> @CRquantum, you could write like this (added carriage return for better formatting): f = Printf.Format("%9.3f"^4\*"\n") io = open("myfile2.txt", "w") for i in 1:10 Printf.format(io, f, 1,2,3,4) end close(io)

Here is the thing, I can manually input 1.0, 2.0, 3.0, 4.0, and print them in one line and in a file called fileout.txt, here is the code,

```
s = Printf.Format(string("%f"^4))
io = open("fileout.txt", "w")
Printf.format(io, s, 1.0,2.0,3.0,4.0)
close(io)

```

Now, if I have an array A,

```
A = [1.0, 2.0, 3.0, 4.0]

```

I was hoping that Printf.format is smart enough to do,

```
Printf.format(io, s, A) 

```

However it cannot recognize A as a 4 element array and print each element as %f.

I wonder, is there a way to print A in one line with the format s?

* * *

Furthermore, if I have two arrays,

```
A = [1.0, 2.0, 3.0, 4.0]
B = [100.0, 200.0, 300.0, 400.0]

```

Now I want in a file say, fileout.txt, have B ± A element by element in one line, like,

```
100.0 +- 1.0 | 200.0 +- 2.0 | 300.0 +- 3.0 | 400.0 +- 4.0 

```

What should I do?

Thank you very much in advance!

---

<div class="post-metadata">

### Author: ![Geoffrey](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@Geoffrey](https://discourse.julialang.org/u/Geoffrey)
#### Post date: [August 24, 2021, 5:36am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/2 "2021-08-24T05:36:09Z")

</div>

Hi, I think that you just need to splat the array using the `...` operator:

```julia
Printf.format(io, s, A...) 

```

---

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 24, 2021, 5:56am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/3 "2021-08-24T05:56:26Z")

</div>

Thank you very much! This is really nice!

One more thing (I added in the question),  
if I have two arrays,  
A = [1.0, 2.0, 3.0, 4.0]  
B = [100.0, 200.0, 300.0, 400.0]

Do you know how to write B ± A element by element in one line, in a file say, fileout.txt,

```
100.0 +- 1.0 | 200.0 +- 2.0 | 300.0 +- 3.0 | 400.0 +- 4.0 

```

Thank you very much!

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 24, 2021, 6:51am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/4 "2021-08-24T06:51:21Z")

</div>

@CRquantum, you can broadcast like this:

```julia
A = [1.0, 2.0, 3.0, 4.0]
B = [100.0, 200.0, 300.0, 400.0]
fmt = Printf.Format("%.3f +- %.3f\n")
open("fileout3.txt", "a") do io
    Printf.format.(Ref(io), Ref(fmt), B, A)
end;

```

---

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 24, 2021, 7:17am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/5 "2021-08-24T07:17:08Z")

</div>

Thank you so much.  
Do you know where can I find some documents about the command Printf.format ? I searched Julia documents but I cannot seem to find much information about it.

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [August 24, 2021, 7:29am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/6 "2021-08-24T07:29:48Z")

</div>

> **[printf](https://en.wikipedia.org/wiki/Printf_format_string)**
>
> The printf family of functions in the C programming language are a set of functions that take a format string as input among a variable sized list of other values and produce as output a string that corresponds to the format specifier and given input values. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (...

[https://www.cplusplus.com/reference/cstdio/printf/](https://www.cplusplus.com/reference/cstdio/printf/)

---

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 24, 2021, 9:09pm UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/7 "2021-08-24T21:09:15Z")

</div>

Thank you very much.  
I did

```
A = [1.0, 2.0, 3.0, 4.0]
B = [100.0, 200.0, 300.0, 400.0]
fmt = Printf.Format("%.3f +- %.3f |")
open("fileout3.txt", "a") do io
    Printf.format.(Ref(io), Ref(fmt), B, A)
end

```

In the output file, I got,

```
100.000 +- 1.000 |200.000 +- 2.000 |300.000 +- 3.000 |400.000 +- 4.000 |

```

Everthing is in one line, this is exactly what I want.

Now, do you know how can I print them in one line on the screen?

I tried

```
println(Printf.format.(Ref(fmt), B, A))

```

However the output seperate the 4 elements,

```
["100.000 +- 1.000 | ", "200.000 +- 2.000 | ", "300.000 +- 3.000 | ", "400.000 +- 4.000 | "]

```

Can I print them on the screen exactly like they are in the file, that is, exactly in one line,

```
100.000 +- 1.000 |200.000 +- 2.000 |300.000 +- 3.000 |400.000 +- 4.000 |

```

It seems I can do a join trick you taught,

```
println(join(Printf.format.(Ref(fmt), B, A)))

```

But is this is only way?

---

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 24, 2021, 9:14pm UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/8 "2021-08-24T21:14:07Z")

</div>

Thank you very much, the … trick is very useful!

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 24, 2021, 9:17pm UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/9 "2021-08-24T21:17:22Z")

</div>

> [@CRquantum](#):
>
> `println(join(Printf.format.(Ref(fmt), B, A)))`

You can also do:

```julia
print.(Printf.format.(Ref(fmt), B, A));

```

_NB: [never ending printing story](https://youtu.be/DCP_d6bO8GI)_ 🙂

---

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 24, 2021, 10:31pm UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/10 "2021-08-24T22:31:53Z")

</div>

Thank you so much! 😀  
We are exploring all possibilities of print 😜

I am sorry I have perhaps [One More Time](https://www.youtube.com/watch?v=C-u5WLJ9Yk4).

Now I want to print 1 2 3 4 directly using a loop, without create a particular array contains 1 2 3 4. Like below I first create a format,

```
n = 4
fmt = Printf.Format("%10i"^n)

```

Then I want to do thing like,

```
Printf.format(fmt, i=1:n) 

```

Is it possible?

I have checked the … trick by @Geoffrey and i did

```
n = 4
fmt = Printf.Format("%10i"^n)
print(Printf.format(fmt, collect(1:n)...))

```

it outputs:

```
       1 2 3 4

```

So it seems working!

If you were to do it, what will you do?

---

<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: [August 24, 2021, 10:59pm UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/11 "2021-08-24T22:59:41Z")

</div>

btw:

```julia
julia> A = [1.0, 2.0, 3.0, 4.0];

julia> B = [100.0, 200.0, 300.0, 400.0];

julia> using Measurements

julia> C = B .± A
4-element Vector{Measurement{Float64}}:
 100.0 ± 1.0
 200.0 ± 2.0
 300.0 ± 3.0
 400.0 ± 4.0

```

not only do they look nice, they behave correctly. (i.e. error propagates)

---

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 24, 2021, 11:38pm UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/13 "2021-08-24T23:38:43Z")

</div>

Thank you very much!  
You are right, the measurement package is nice.

Uhm, here the thing is more about you know, also show those stuff in one line, and with some format like “|” as separation symbol.

```
100.000 +- 1.000 |200.000 +- 2.000 |300.000 +- 3.000 |400.000 +- 4.000 |

```

But I am sure with your suggestion and the suggestions here, I can achieve the purpose.

---

<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: [August 24, 2021, 11:41pm UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/14 "2021-08-24T23:41:17Z")

</div>

```julia
julia> join(C, " | ")
"100.0 ± 1.0 | 200.0 ± 2.0 | 300.0 ± 3.0 | 400.0 ± 4.0"

```

---

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 25, 2021, 12:44am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/15 "2021-08-25T00:44:16Z")

</div>

That is nice!  
By the way, is there a way in Julia that, in @printf that I can move the some particular number of spaces, like  
[https://stackoverflow.com/questions/25609437/print-number-of-spaces-using-printf-in-c](https://stackoverflow.com/questions/25609437/print-number-of-spaces-using-printf-in-c)

The highest ranked answer said that,

```
printf("%*c", n, ' ')

```

can print n spaces. So in Julia, I expect that if I set n = 10, like

```
 printf("%*c", 10, ' ')

```

But it seems it does not work, do you know why?

---

<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: [August 25, 2021, 12:48am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/16 "2021-08-25T00:48:57Z")

</div>

> [@CRquantum](#):
>
> do you know why?

because Julia is not C? I thought padding is solved with `%10i` no?

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [August 25, 2021, 12:57am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/17 "2021-08-25T00:57:29Z")

</div>

```julia
julia> " " ^ 10
" "

```

but if your goal is to pad strings, look at [`lpad`](https://docs.julialang.org/en/v1/base/strings/#Base.lpad) and [`rpad`](https://docs.julialang.org/en/v1/base/strings/#Base.rpad)

---

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 25, 2021, 5:43am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/18 "2021-08-25T05:43:51Z")

</div>

Thank you @jling ! padding should work.  
I am asking this because from Julia documentation, [Printf · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Printf/)  
it seems indicating that Julia support all the commands of printf like in C, so I think any printf works for C should work the same as for Julia. But it seems Julia does not 100% support all the command of printf. But it is OK.  
Thank you @giordano too!

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [August 25, 2021, 8:29am UTC](https://discourse.julialang.org/t/how-to-print-some-elements-of-array-in-one-line-in-a-file/66895/19 "2021-08-25T08:29:53Z")

</div>

As an overall comment, you seem to be spending a lot of time manually trying to get the formatting of some data in plain text files right, which to me feels a bit like a Fortran-1980s-way of going about things. There are excellent packages in Julia for formatting your data and outputting it to all sorts of different backends. I would recommend you take a look at

[https://github.com/ronisbr/PrettyTables.jl](https://github.com/ronisbr/PrettyTables.jl)
