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

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

and

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!

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

Printf.format(io, s, A...) 
2 Likes

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!

@CRquantum, you can broadcast like this:

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;
2 Likes

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.

https://www.cplusplus.com/reference/cstdio/printf/

1 Like

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?

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

You can also do:

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

NB: never ending printing story :slight_smile:

1 Like

Thank you so much! :grinning:
We are exploring all possibilities of print :stuck_out_tongue_winking_eye:

I am sorry I have perhaps One More Time.

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?

1 Like

btw:

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)

2 Likes

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.

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

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

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?

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

1 Like
julia> " " ^ 10
"          "

but if your goal is to pad strings, look at lpad and rpad

2 Likes

Thank you @jling ! padding should work.
I am asking this because from Julia documentation, Printf · The Julia Language
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!

1 Like

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

4 Likes