Page 1 of 1

How to write thank's to use "Prolog" ?

Posted: Mon Apr 28, 2014 7:40 am
by beber005
Hello everyone, :)

I have a rather peculiar problem. Everyone knows which is in the "Advanced" tab "prolog" tab! I know it can do a treatment once before starting to read the lines of my file. I build a file that has a header but this one should not at all be repeated as many times as there are lines. My idea has been to put this snippet of code into "Prolog". But I'm feeling that "Prolog" can only write when there is an error. I do not know if I've done well understand because this is not easy to explain.

Thanks a lot for your help because I don't see the solution with this problem :oops:

Re: How to write thank's to use "Prolog" ?

Posted: Mon Apr 28, 2014 7:54 am
by declanr
Are you just asking how to put a header on records output?

If so there are a lot of posts about it.
Just stick:

Code: Select all

 iHeader = 0;
In the prolog and then:

Code: Select all

If ( iHeader = 0 );
      Output header info
       iHeader = 1;
EndIf;

In the data tab (Assuming that is where you do the output)

Re: How to write thank's to use "Prolog" ?

Posted: Mon Apr 28, 2014 8:25 am
by beber005
Ok thanks for your help it works :)
But why it's impossible to write directly in "Prolog" tab ?

Re: How to write thank's to use "Prolog" ?

Posted: Mon Apr 28, 2014 8:35 am
by declanr
Its not impossible. But if outputting to the same file; each subsequent tab will overwrite the last.

The file is closed at the end of each tab.

Re: How to write thank's to use "Prolog" ?

Posted: Mon Apr 28, 2014 8:38 am
by Alan Kirk
beber005 wrote:Ok thanks for your help it works :)
But why it's impossible to write directly in "Prolog" tab ?
It's not that you can't write in the Prolog tab (and writing from the Prolog tab is not restricted to errors), it's just that there are two limitations. (Edit: Which Delcan just beat me to.)

The primary one is that each of the tabs (Prolog, Metadata, Data and Epilog) are regarded, in some sense, as being separate "sub-programs", if you like. What this means is that if you write a file of a specific name in the Prolog, and then do a further write to the same file in the Metadata, then the Metadata write will blow away the Prolog file and replace it with a completely new one.

If you then write to the same filename again in the Data tab, it will blow away the Metadata tab's file.

And, you guessed it, if you write to the same filename in the Epilog tab, it will blow away the file that you wrote in the Data tab.

The only way around this is to write to different file names and then use ExecuteCommand to run a batch file to merge them together. For the sort of thing that Declan has described that's an overcomplicated way of dealing with it; it's much easier to just follow his suggestion of writing the header row in the Data tab the first time you need to do the output.

It's fecking annoying and one of the features that many developers have wanted for ages (and which we see no signs of getting) is an AsciiAppend function which would let us use the one file on all four tabs. If we ever get that you'll be able to write your header in the Prolog. In the meantime, better to follow Declan's advice.

The second thing is something that it appears from your post that you do in fact "get", but you need to get it very clear in your mind. The Prolog code, all of the Prolog code, runs before the first row of the data file is even opened, much less read. It has to, because there are commands that you can use in the Prolog to change the data source to an entirely different file. The consequence of this is that you can never use code in the Prolog tab to check for errors in the data source itself - you can only do that in the Metadata or Data tabs. The only error you can check for in the Prolog is whether the source file (assuming that your source is a text file) actually exists. If it doesn't, then you can write an error log and quit the process. But you can never check the contents of the file itself, not even the file headers, in the Prolog.

Re: How to write thank's to use "Prolog" ?

Posted: Mon Apr 28, 2014 8:49 am
by beber005
Hi Alan :)

First thanks a lot for this explication I understand now. And yes I know the Prolog code, it is running before the first row of the data file. But my problem was How to write juste once a header. That's why I was thinking to Prolog tab.

In any case thank you for your quick responses you have released me :D