Wednesday, September 6, 2017

Record markers to ASCII (and vice versa)

Credits to Thomas Mantooth.

The UniData delimiters are in reverse order of their hierarchy. So, going in the other direction...

@RM (Record mark) = CHAR(255)
@FM (Field mark) = CHAR(254)
@VM (Value mark) = CHAR(253)
@SM (Sub-value mark) = CHAR(252)
@TM (Text mark) = CHAR(251)

One neat trick you can make use of with this comes into play when you use the REMOVE command. It assigns a value that indicates what type of delimiter it returns. Most of the time, you don't care what the specific delimiter is - you're just looking to see if it's zero, which is returned at the end when there's no delimiter. However, if you're dealing with something that can have multiple delimiters, you can use REMOVE and the delimiter value to build a new dynamic array with the same delimiters very simply.

LOOP
    REMOVE X.VALUE FROM XL.ARRAY SETTING X.DELIMITER
    * Do something to manipulate the data
    XL.NEW.ARRAY := X.VALUE
UNTIL NOT(X,DELIM) DO
    XL.NEW.ARRAY := CHAR(256 - X.DELIMITER)
REPEAT