Assigning a null value to a variable

Hi,

I am trying to write a variable x to a matrix, that is output ultimately as a CSV file. Sometimes x will have a numerical value and other time no value (a null in my old language VBA). The question is how to assign a null value to x. I downloaded Missings.jl that has the “nothing” value, but this doesn’t work (see code and output below). There must be something very basic I am missing

Thanks Peter

import Pkg; Pkg.add("Missings")

for i = 1:10

    if i < 5
        x = nothing
    else
        x=1
    end
@show x
end

x = nothing
x = nothing
x = nothing
x = nothing
x = 1
x = 1
x = 1
x = 1
x = 1
x = 1

I’m pretty unsure what you expected would happen here. Can you clarify?

Note that you maybe want the value missing, not nothing, if you’re using Missings.

2 Likes

Hi,

Thanks for quick response. I tried missing and got same result. What I want is for x = null (just a blank empty space) when i < 5. I hope this is clear.

Thanks Peter

First things first - both nothing and missing are available without using the Missings.jl package. In julia, these two values are semantically different (I don’t have the link, but there’s a great GitHub issue describing them as “the software engineer’s null” and “the data scientist’s” null or something).

One example of how they’re different is that 1 + missing is missing where 1 + nothing throws an error.

None of this exactly answers your question, I think your question may be arising from some misunderstanding. x = nothing in fact assigns the variable x to the value nothing. Perhaps it’s just that you’re using @show instead of println()?

2 Likes

I should also mention the excellent CSV.jl package if you’re going to be writing csvs from matrices, and that package will deal correctly with missing for sure (those will be empty by default, but there’s an option to specify). It might deal with nothing the same way, though I don’t know that for sure.

Kev,

Thanks for response. I tried println() but same result. In my actual program I assign “x” to a matrix location, then make a DataFrame from the matrix and then use CSV,write(…) command to make the CSV file. Maybe I am missing an option in the the CSV.write arguments … need to explore further.
Thanks Peter

I forgot to mention that my matrix in my actual program is initialized with zeros, so it probably doesn’t accept values that aren’t numbers. Is there a way to define the matrix to accept both real numbers and blanks(null vamue)?

I found below works. Good old trial and error. I am actual program I had the declare my array as Any as below and then assigned the element ’ ’ when I wanted to write/print a blank. Hope it is useful to someone else.

 Matrix_Detailed_GH      = Array{Any}(missing, Hours_Total,182) # actual matrix


# write a null
import Pkg; Pkg.add("Missings")

for i = 1:10

    if i < 5
        x = ' '
    else
        x=1
    end
println(x)
end

There is no predefined value called null in Julia, so it is not clear what you want here. Rather than using ' ' for missing values, missing or nothing (depending on the semantics you need) would allow you to interface with more functionality in the ecosystem.

2 Likes

Yes, allowmissings function does that, eg. allowmissings(zeros(10)).

1 Like

Thank you all for the responses. I now have a couple of options. Peter

1 Like

Another option if your matrix is of Float64 would be to use NaN, though I’m not sure how CSV.jl freaks with those

CSV.jl is world class. It can handle anything!

2 Likes

Agree completely! Can handle for sure, I just don’t know if they’re handled by default the way @Peter-Jolly wants.