Error with semi-colon, using a loop for writing to a file

Julia is reporting a strange syntax error with semi-colon, when I am trying to use a loop for writing to a file.

function print_selected_entries(select::Array{Int64,1}, v::Array{Float64,1}, file::String)
N=size(select);
f = open(file, “w”);
for i=1:N
if select[i]==1
print(f,v[i],“\n”);
end
end
close(f);
end

This returns the strange output

julia> print_selected_entries(select,omegas,“t.txt”);
ERROR: MethodError: no method matching (::Colon)(::Int64, ::Tuple{Int64})
Closest candidates are: …

Stacktrace:
[1] print_selected_entries(::Array{Int64,1}, ::Array{Float64,1}, ::String) at /data/dass/Julia/t.jl:4
[2] top-level scope at none:0

Change “N=size(select)” to “N=size(select,1)”.

The error is coming from the “for i=1:N” line. The way you have it now, N is a tuple that looks like (number_rows, number_columns). Instead, you want just the number of rows.

1 Like

Also, please use backticks to quote code:

2 Likes