ERROR: LoadError: UndefRefError: access to undefined reference

So I have the following function

function regionextract(data::Array{<:Real},coord::Array)

    nlon1 = length(coord[1]); nlon2 = size(data,1);
    nlat1 = length(coord[2]); nlat2 = size(data,2);
    nt = size(data)[3:end]; ndim = ndims(data);

    if ndim == 2; data = data[coord[1],coord[2]];
    elseif ndim >= 3;
        data = reshape(data,nlon2,nlat2,:);
        data = data[coord[1],coord[2],:];
        data = reshape(data,Tuple(vcat(nlon1,nlat1,nt...)));
    end

end

However, I have the following error

ERROR: LoadError: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex at ./array.jl:729 [inlined]
 [2] macro expansion at ./multidimensional.jl:699 [inlined]
 [3] macro expansion at ./cartesian.jl:64 [inlined]
 [4] macro expansion at ./multidimensional.jl:694 [inlined]
 [5] _unsafe_getindex! at ./multidimensional.jl:690 [inlined]
 [6] _unsafe_getindex(::IndexLinear, ::Array{Real,3}, ::UnitRange{Int64}, ::UnitRange{Int64}, ::Base.Slice{Base.OneTo{Int64}}) at ./multidimensional.jl:684
 [7] _getindex at ./multidimensional.jl:670 [inlined]
 [8] getindex at ./abstractarray.jl:981 [inlined]

which I have traced to the line

data = data[coord[1],coord[2],:];

I’ve seen other topics with the same error, but I am not sure if I am doing my types correctly. I’ve checked on both coord[1] and coord[2] and they both give reasonable values that are within the bounds of the data array.

It is very likely that you have not initialized elements of coord, but it is hard to say more without a self-contained example. Cf

julia> Array{Any}(undef, 1)[1]
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(::Array{Any,1}, ::Int64) at ./array.jl:787
 [2] top-level scope at REPL[1]:1

What do you mean exactly by coord not being initialized?

coord[1] and coord[2] both exist: doing @info coord[1] gives me 345:456, for example.

I was just guessing based on the information you provided. Again, it is hard to say more without an MWE.

Update. Seems to be that I defined things in the parent function wrongly :expressionless: so yeah, I think this topic can be closed haha.