Initialize one time with Nz and Staticvar IS working fine

Hello All,

I am trying to initialize a variable during 1st run and wrote below code to test. It is not working (i.e. It is not executing printf statement inside IF). Appreciate your inputs.

_SECTION_BEGIN("Test1");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

printf("Before IF\n");

if(Nz(StaticVarGet(Name()+"c1")) == 0)
{
StaticVarSet( Name()+"c1",1);
printf("Inside IF\n");
}

printf("After IF\n");

_SECTION_END();

Thanks
Shiva

Moderator comment: the code works absolutely fine. The only mistake that OP is doing is not knowing how to debug and how to use TRACE

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

That printed text inside your if statement is visible for a blink of an eye only.
Printf does not record text.
Use _Trace function instead to record messages.
http://www.amibroker.com/guide/afl/_trace.html

It did work for first time only.

So, you need to remove that StaticVar for the code inside it to be executed again. Also, learn the usage of * as mentioned in StaticVarRemove.

Having that said, do not keep that StaticVarRemove in your code all the time. That would be like shooting yourself in the foot and the intention of "Execute that Once" won't be served.

Hi
If you like the static var to start from zero you should write with this way
Nz(StaticVarGet(Name()+"c1")),0)

Default value is zero already!


???

I was thinning is Empty

Already mentioned:

We do not know what OP is doing there or thinking.


Empty != Null

Empty (or "") applies to StaticVarSetText or StaticVarGetText like an empty string, and, Null applies to StaticVarSet or StaticVarGet kind of similar to should be a number instead NaN (Not a Number).

Default ValueIfNull of Nz function is zero.
Look it up
http://www.amibroker.com/guide/afl/nz.html

Read first sentence

And answer how to record has been given in 2nd post already.
Printf does not record, _trace does.
So I don't what's still to discuss.

It is about visibility here and clear (all) facts.

P.S. I read that line that you disrespectfully wrote, then edited. :laughing:

You seem to have reading problems.
Once again read first sentence of first post.
Then read second post of this thread in regards to visibility of text when trying to initialize one time.

Of course you could do non sense like this

if(Nz(StaticVarGet(Name()+"c1")) == 0)
{
    StaticVarSet( Name()+"c1",1);
    StaticvarSetText("InsideIf", "Was executed inside IF\n");
}

printf(StaticvarGetText("InsideIf"));

Or just ...

var = Nz(StaticVarGet(Name()+"c1"));

if (var == 0) {
	StaticVarSet( Name()+"c1",1);	
}

// Check outside of 'if' statement
printf("%s", StrExtract("Not initialized,Was initialized", var));

True!

Would be no non-sensical, if written like this:

// https://forum.amibroker.com/t/initialize-one-time-with-nz-and-staticvar-not-working/34917/11
// Edited by Cougar

EnableTextOutput( 0 );

chartId = Name() + GetChartID();

execChk = Nz( StaticVarGet( "execChk" + chartId ) );
if( !execChk ) {
    StaticvarSetText( "insideIf" + chartId, "Greetings from inside\n" );
    StaticVarSet( "execChk" + chartId, 1 );
}

insideIfStr = StaticVarGetText( "insideIf" + chartId );

And I was referring to something like this "only" during development (not in production) just to reset StaticVars:

EnableTextOutput( 0 );

chartId = Name() + GetChartID();

// StaticVarRemove( "execChk" + chartId );
execChk = Nz( StaticVarGet( "execChk" + chartId ) );
if( !execChk ) {
    StaticvarSetText( "insideIf" + chartId, "Greetings from inside\n" );
    StaticVarSet( "execChk" + chartId, 1 );
}

insideIfStr = StaticVarGetText( "insideIf" + chartId );

I read that when you posted it, did not fuzz! However, since you insist, re-read and replying now.

Probably, someone here does need to revisit that good old printf function reference.

printf and StrFormat behave identically except that printf writes output to the window, while StrFormat does not write anything to output window but returns resulting string instead.


That statement is not correct.

printf( string ) works, so does printf( "%s", string )!

trash



P.S.

I knew @fxshrat - The Perfectionist, did not know turned to a virtual Ophthalmologist as well. Thanks for the advice, for now I can read well (especially stuff written between lines).

Of course it is correct.

Just current text output (of current AFL run) is seen in interpretation window but no past one.
Interpretation window output is not keeping memory of old text output been printed within an 'if' statement returning true (single time). So most of the time you can't see it because execution is too fast and just being viewable within blink of an eye (re-read 2nd post of this thread once again). Text of if statement being executed before is not "recorded" there (not being kept in Interpretation window).

On the other hand _trace writes to debug viewer where (all) past recorded output can be viewed.
All records are kept there.


Huh, what's your message?

Thanks for the laugh.
As usual it is just repetition of already written code.
Whether adding chartID or not is up to the user of first post.
It is rather about making printf to output text kept in memory.

Now take another one hour or two ones to write long essay.

As mentioned printf behaves identically like StrFormat. Do not steer this to printf vs _TRACEF! This thread was never meant to be so.

If StaticVarSet inside an one-time executed code, unless reset, won't execute that inside code again. Does not matter whether you use printf or _TRACEF or _TRACE.

Sometimes, it is better to swallow rather than frantically throwing at others.

I wonder how you lost that old dignity of yours. Sorry, you're making fun of yourself!

Troll of the month or what?

Remember, you started it with:

This was posted:

Because:

And you're unnecessarily dragging it to printf vs _TRACEF wherein the conversation was never meant to be so.

Been added after my question marks.
Besides there is no need for using Staticvarremove anyway.

Yes, because of first post. All that matters.

Did not know, one can turn mean just to prove their point. Anyways, good social learning!