Readdlm() Reading Out of Order

Hi, I’m a somewhat novice Fortran programmer (I use Fortran 90 for physics research, but have had no formal training as a programmer) dipping my toes into Julia as a possible replacement language. Naturally, I started off trying to replicate basic Fortran tasks, like reading input variables from text files. I created a file “param.inp” as follows:

1 2
3 4

This represents a normal input file, with one or two values per line, delineated by whitespace or newlines. After a fair amount of bashing my head against Julia’s unfamiliar read() function, I eventually figured out that to read in each value as a variable, I should use readdlm, which automatically assumes whitespace separation between values. I tried the following test script:

using DelimitedFiles

a, b, c, d = readdlm("param.inp")
println(a, b, c, d)

The output of this script is: 1324

Notice the issue? readdlm() read the first value of the first line (1) and assigned it to a, then the first value of the second line (3) and assigned it to b, then the second value of the first line (2) went to c, and the second value of the second line (4) went to d. I expected it to read across the first line before moving to the second, so that a = 1, b = 2, c =3, and d = 4. Yet, b and c are switched around. Could someone explain why this is happening? It’s completely unlike Fortran’s read() behavior.

readdlm gives you a matrix, just like Fortran, Julia’s matrix is column major, so the first 2 elements will be 1,3:

julia> readdlm("./test")
2×2 Matrix{Float64}:
 1.0  2.0
 3.0  4.0
2 Likes

I think you could also read the text file as follows with Base readline():

io = open("param.inp", "r");
a, b = parse.(Int64, split(readline(io)))
c, d = parse.(Int64, split(readline(io)))
close(io)
1 Like