Thursday 3 August 2023

How to include a backslash ('\') in a string

Someone wondered how they could convert a string holding a file name (such as 'Z:/ABC/DEF.TXT') into a file name that the file system would recognise ('Z:\ABC\DEF.TXT'). This turned out to be unexpectedly difficult, primarily because Priority regards the backslash (\) as an escape character and there is no real mechanism for obtaining or assigning a single character to a variable.

The first part - replacing the forward slashes with hyphens - was simple.

:PAR3 = 'Z:/ABC/DEF.TXT'; :SLASH = '-'; LABEL 10; :PAR1 = STRPIECE (:PAR3, '/', 1, 1); GOTO 20 WHERE :PAR1 = :PAR3; :PAR2 = STRPIECE (:PAR3, '/', 2, 9); :PAR3 = STRCAT (:PAR1, :SLASH, :PAR2); LOOP 10; LABEL 20; /* at this stage, :PAR3 will be Z:-ABC-DEF.TXT */
I could output the string to a file then use the FILTER program to change the hyphens into blackslashes, but then the user would be faced with the problem of getting the string out of the file.

This morning, the answer hit me when I was doing something else entirely. The problem of the backslash is the same as the problem of the dollar sign, so the solution is the same: create a message (in this case, 11) whose text is simply \. The code now becomes
:PAR3 = 'Z:/ABC/DEF.TXT'; SELECT ENTMESSAGE ('$', 'P', 11) INTO :SLASH FROM DUMMY; LABEL 10; :PAR1 = STRPIECE (:PAR3, '/', 1, 1); GOTO 20 WHERE :PAR1 = :PAR3; :PAR2 = STRPIECE (:PAR3, '/', 2, 9); :PAR3 = STRCAT (:PAR1, :SLASH, :PAR2); LOOP 10; LABEL 20; /* at this stage, :PAR3 will be Z:\ABC\DEF.TXT */