base.Channel.setDelimiter()

setDelimiter() is an optional method used to specify a delimiter other then the default one. The default delimiter is specified in the corresponding environment variable (DBDELIMITER) or, if it is not set, "|" is used. The method requires a single argument.

Syntax:

chan_obj.setDelimiter("^")

Usage example:

CALL chan_obj.setDelimiter("^")

If no argument is specified, the delimiter is set to NULL. In this case, the "|" symbol will not be used by read() and write() methods as an escape character.

It is possible to make the application read and write in Comma Separated Value format. To switch to this mode, pass CSV as the delimiter to the setDelimiter() method.

CALL chan_obj.setDelimiter("CSV")

There are several differences between this format and the standard Channel format, they are listed below:

Input/Output formatting

Input and output operations are mostly performed with formatted pieces of text, each line of which represents one record. A delimiter specified with setDelimiter() indicates the end of each field value. The default delimiter is the pipe (|) symbol. The fields that do not have any values should also be bordered by delimiters.

The following is the content of the parse.txt file that is used in the code of the program:

1|John Smith|123445|London|

2|Samanta Black||Huston|

3||34567|New York|

|Tim Copper|67889|Dublin|

|Dolores Crew|||

6|Joseph Lee|9993338|Chikago|

read() or write() will treat empty fields as NULL:

MAIN
 DEFINE  chan_obj base.Channel
 DEFINE  cTxt RECORD f1,f2,f3,f4 STRING END RECORD
 
    LET chan_obj = base.Channel.create()
    CALL chan_obj.openFile("parse.txt","r")
    WHILE chan_obj.read([cTxt.*])    
       DISPLAY cTxt.f1,"|",cTxt.f2,"|",cTxt.f3,"|",cTxt.f4
    END WHILE
    
END MAIN 

Generally, the read()and write()methods process the data according to the same formatting rules as the LOAD and UNLOAD SQL statements. The only difference is that here it is impossible to specify a NULL delimiter, because an empty DBDELIMITER environment variable is treated as the default pipe symbol.

The backslash symbol (\) can be used as an escape character, but only in case a delimiter is used. write() will escape any backslash, delimiter and other special characters. read() will convert any escaped character (\char combination) into char.

The next extract of the code will write one field value, containing a backslash, a delimiter and a line-feed character. In Querix 4GL, backslash is treated as an escape character; therefore, it must be doubled to be added to a character string. The line-feed character in 4GL is \n:

CALL chan_obj.SetDelimiter("^")
CALL chan_obj.Write("a\\bc^cd\nEEE")

When this code is executed, the text file will get this string:

a\\bc\^cd\

EEE^

 

Contact Us

Privacy Policy

Copyright © 2024 Querix, (UK) Ltd.