Question How can I read "\" as a character in progress

Pavan Yadav

Member
Hi All,

I have a requirement where I need to replace "\" with "_" for all the occurrences in field or variable.

Code:
def var abc as char initial "'abc\test1'".
def var xyz as char.
disp abc format "x(25)".
xyz = replace(abc,"\","_").
disp xyz.

This code doesn't recognize "\" in the variable. Please advice how this can be achieved.
 

Cringer

ProgressTalk.com Moderator
Staff member
I believe it's "\\". If that doesn't do it then "~\" might be the solution. It's Monday - my memory isn't great... ;)
 

Pavan Yadav

Member
I tried that too but it's not working :( , in actuals even the simple display is not correct. Output for above code is something like this:
abc -- 'abc est1'
xyz -- 'abc est1'

Sorry, I forgot to mention env:
OE 10.1C
Unix
 

Cringer

ProgressTalk.com Moderator
Staff member
Maybe it's your environment as your code works absolutely fine on 11.2.1. :(
 

Pavan Yadav

Member
U tried that on Unix or Windows env?
As I could think of, it might be due to Unix env.

Or is there any way to make this work on Unix ?
 

Osborne

Active Member
Yes, the problem is UNIX as \ is not allowed if it appears in code. Does doing anything like this help?:
Code:
def var abc as char.
def var xyz as char.

abc = "abc" + CHR(92) + "test1".
disp abc format "x(25)".
xyz = replace(abc,CHR(92),"_").
disp xyz.
 

Pavan Yadav

Member
Osborne, this is also not working.. I am not assigning values to the variable or fields. I am just retrieving it and making a replacement on above criteria.
So, I need to evaluate the existing values.
 

LarryD

Active Member
Osborne's code works fine on Linux 10.2B.

This does too:
Code:
def var abc as char.
def var xyz as char.

abc = "abc~\test1".
disp abc format "x(25)".
xyz = replace(abc,"~\","_").
disp xyz.
 

Cringer

ProgressTalk.com Moderator
Staff member
If you already have the data stored in the db then you need to work out what character is actually there.
Code:
def var lv-i as int no-undo. 


def var abc as char initial "'abc\test1'".


do lv-i = 1 to length(abc):
  message substring(abc,lv-i,1) ":" asc(substring(abc,lv-i,1)) view-as alert-box. 
end.

the ASC() function is the ascii value of each character in the string. You should be able to work out then which character is in the string in the db. You can then use that in the CHR() as per Osborne's code.
 

Pavan Yadav

Member
Larry, I guess I am unable to clear you my requirement.
Osborne's code works fine, But it will not work in my case.
I am not assigning the values to variable or field. But, evaluating the already existing values.

So I can't use
Code:
 abc = "abc" + CHR(92) + "test1".

But, I have to use abc = "abc/test1" only which already exist in a field. Hope you got my point.
 

Pavan Yadav

Member
Cringer, this statement is having the same issue, as it's not at all evaluating "\" seperately.
Ideally, lenght(abc) should be 11, but it shows 10 only with 5-character as " : 9" and 6th character is "e".
 

Osborne

Active Member
Pavan, are you saying there is a value in a database field that is "abc/test1", and you want the program to check for this exact value and if it exists replace the "\"? If so, then would this work?:
Code:
def var abc as char.

abc = "abc" + CHR(92) + "test1".
IF dbtable.dbfield = abc THEN
   dbtable.dbfield = replace(dbtable.dbfield,CHR(92),"_").
If not, can you give an example of what you are trying to do with the field.
 

Cringer

ProgressTalk.com Moderator
Staff member
Have you tried my code against a value in your database? If you have the same issue there then I think your data is broken.
 

LarryD

Active Member
I think that the problem is that on *nix, unless the backslash is escaped ( with a tilde "~t" or second backslash "\\") it will always be interpreted as the *nix equivalent.

In the above, it treats "\t" as a tab. \\t or ~\t will be interpreted properly. Same would hold true if it was "\n" (linefeed) etc.

If it's coming from a text file or internal variable, then you have to escape it in the raw data before doing any string manipulations.

If it's in the db, I'm not really sure what you can do because I believe it's stored now as the interpreted character (I could be wrong here, but it would be easy enough to test). You could probably write some code looking for backslash character and change it going thru each printable character in a loop, but beyond that I'm not sure how else you could resolve it.
 
Last edited:

TomBascom

Curmudgeon
The code posted in your original post:
Code:
def var abc as char initial "'abc\test1'".
def var xyz as char.
disp abc format "x(25)".
xyz = replace(abc,"\","_").
disp xyz.

Does not work the way you expect because the \ character is an "escape" character -- it tells the 4GL that the next character is special.

You can tell the 4GL that you literally want a backslash in several ways. One is to use chr(92) as shown. Another is to "double-escape" like so: "abc\\test1". Or you can use the alternative escape character "abc~\123".

If there is a value already in a database field called "abc" that you are trying to replace with "_" then the REPLACE() finction should look like: REPLACE( abc, "~\", "_" ).
 

Pavan Yadav

Member
Thanks All...!!
I am sorry, it was my mistake only .

Because it didn't work correctly when I was trying with values(abc\test) assigned to variable as mentioned in above example.
But, "~\", char(92), "\\" all worked perfectly when i tried it on my Live scenario issue, with values in Databases.
 
Hello Everyone, hope you all are well. :)

I tried this code in procedure editor and it works fine:
Code:
def var abc as char initial "'abc\test1'".
def var xyz as char.
disp abc format "x(25)".
xyz = replace(abc,"\","_").
disp xyz

But if i am trying the same in CHAR programming then it requires ~ with \, why?. I found the same problem with OS-RENAME also.

Please suggest.

Thanks & Regards!
Rajat.
 

TomBascom

Curmudgeon
When naked \ "works" it is an accident.

Best practice is to always escape the backslash, tilde, ampersand and brace characters. These guys: \~&{}

The function of \ is explained above. If you are on Windows you sometimes "get away with it" because some of the places where it escapes text are specific to UNIX usage -- but there is no clear and easy way (that I know of) to say when it will bite you and when it won't. So just always escape it.

~ is Progress' general purpose escape. It always works the same regardless of if you are on Windows or UNIX. It is the better character to use to say "I am escaping the next character!".

& is used in some strings like menu accelerators and in SUBSTITUTE() to indicate where replacement should occur. It is also used in include file arguments.

{ is especially nasty -- it tries to treat the next bit of text as an include file name. It can also be part of an include file argument list. } closes the include file or include argument and I usually escape it whenever I escape { just for completeness.

It is also sometimes necessary to escape / if the string is /* but you don't really want it to be a comment.

Remember that a doubled escape character means to really use a single character.

Quotes are fun too:
Code:
display
  "plain string."  skip
  "with ' inside."  skip
  "with "" inside."  skip
  'switch " quote.'  skip
  "go crazy """"!"  skip
  "escaped ~"."  skip
.
 

TomBascom

Curmudgeon
Forgot to mention...

The form \### is also used for the octal representation of a character. So
Code:
display "\052".
Results in an asterisk being displayed.

Interestingly ~052 also works :)
 
Top