how to deal with "nil"?

Moderator: admin

how to deal with "nil"?

Postby clodhoppes » Sun Sep 05, 2010 9:34 am

Nil has been a bother to me. I wonder how to tell whether a parameter is nil or not. Could I write like this:
......
local pt ;
if i>1 then pt[period]=nil;
else pt[period]=source.high[period];
end
if pt[period]~= nil then core.host:execute ("drawLabel",.....

What's wrong with the condition "~=nil"?
clodhoppes
 
Posts: 26
Joined: Sun Aug 15, 2010 8:11 pm

Re: how to deal with "nil"?

Postby Nikolay.Gekht » Sun Sep 05, 2010 10:42 pm

nil is a value which can be assigned to almost all lua variable or table item and means "nothing". The Lua's documentation about the "nil" values is here: http://www.fxcodebase.com/documents/ind ... a/2.2.html

As about the example above, I see no problems in these code lines, but to explain what happen I have to see the whole code, for example what pt is.
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: how to deal with "nil"?

Postby clodhoppes » Mon Sep 06, 2010 7:43 am

Thank your for your explanation. The pt[period] is an integer, and I want to know what value the computer fills in pt[period] when pt[period]=nil. According to my results, the condition" if pt[period]~=nil" does not work. It seems that all pt[period] have got a value not equal to nil.
clodhoppes
 
Posts: 26
Joined: Sun Aug 15, 2010 8:11 pm

Re: how to deal with "nil"?

Postby Nikolay.Gekht » Tue Sep 14, 2010 11:59 am

Probably we speak about different things. Here is a simple example of the "indicator" which can be executed under debugger to check nil comparison for lua tables:

Code: Select all
function Init()
end

function Prepare()
end

function Update(period, mode)
    local pt = {};
    pt[1] = 1;
    if pt[1] == nil then
        core.host:trace("pt1 is nil");
    end
    if pt[1] ~= nil then
        core.host:trace("pt1 is not nil");
    end
    pt[1] = nil;
    if pt[1] == nil then
        core.host:trace("pt1 is nil");
    end
    if pt[1] ~= nil then
        core.host:trace("pt1 is not nil");
    end
end


The output is:
pt1 is not nil
pt1 is nil


However, if pt is input or output indicator stream or integer array (see core.array), the comparison with nil will not work since all these things are numeric vectors. In that case, for streams you have to use stream:hasData(period) to check whether stream is filled for the particular period.
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC


Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 70 guests