Highlighted
Absent Member.
Absent Member.
806 views

[archive] Inspect Replacing

[Migrated content. Thread originally posted on 16 December 2004]

Hello all

Does anyone have know a easy way to inspect a PIC X(500) string for the following char and converting them

@ Convert to &at;
< Convert to <
> Convert to >
? Convert to '


Fx. the string ?abc@mail.com? must be converted to ?abc&at;mail.com?

The only way I know is to check the string character by character, and check if the current char must be converted and so on.
I have checked the documentation but it appears that it is only possible to convert from/to if the source and destination item is the same size. And this is not the case.

Thanx
Kim
0 Likes
6 Replies
Highlighted
Absent Member.
Absent Member.

RE: [archive] Inspect Replacing

Unfortunately, you will have to wrap a PERFORM loop around a reference modification and a STRING with pointer.
It would be great to see INSPECT REPLACE enhanced to work more like the Visual Basic REPLACING verb where there are no size limits on replacements. Doing this would certainly make INSPECT REPLACING less efficient, but worth it I think.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Inspect Replacing

A couple of comments on this.

First of all, such an enhancement would have a significant performance impact. Now, having said that, presumably this is not an activity that will be repetedly performed, so...

The other issue is that VB uses a dynamic string object, thus, it is one fundamental problem less there. In COBOL you operate on static sizes (PIC X(WhateverButStatic)), thus it would be impossible to form this mechanism without putting some constraint to it.

If, as proposed with the next standard, COBOL would get dynamic arrays however...
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Inspect Replacing

This is what I've used for creating XML files. Didn't catch the @ replacement though.

IF CXML-TAG-VALUE NOT = SPACE
SET XML-SIZE TO SIZE OF CXML-TAG-VALUE
MOVE 0 TO XML-COUNT
INSPECT CXML-TAG-VALUE TALLYING XML-COUNT
FOR ALL "" "&"
IF XML-COUNT > 0
PERFORM CXML-QUOTE THRU CXML-QUOTE-EXIT.

CXML-QUOTE.
MOVE 0 TO XML-COUNT.
INSPECT CXML-TAG-VALUE
TALLYING XML-COUNT FOR CHARACTERS BEFORE "
BEFORE ">"
BEFORE "&".
IF XML-COUNT = XML-SIZE
MOVE 0 TO XML-COUNT.
IF XML-COUNT > 0 AND
MOVE XML-COUNT TO XML-END
IF XML-END XML-SIZE
MOVE 1 TO XML-END
END-IF
STRING CXML-TAG-VALUE(1:XML-END) DELIMITED BY SIZE
INTO XML-REC
WITH POINTER XML-PTR
MOVE SPACE TO CXML-TAG-VALUE(1:XML-END)
CALL "C$JUSTIFY" USING CXML-TAG-VALUE "L"
EVALUATE CXML-TAG-VALUE(1:1)
WHEN "
INTO XML-REC WITH POINTER XML-PTR
WHEN ">" STRING ">" DELIMITED BY SIZE
INTO XML-REC WITH POINTER XML-PTR
WHEN "&" STRING "&" DELIMITED BY SIZE
INTO XML-REC WITH POINTER XML-PTR
END-EVALUATE
SUBTRACT 1 FROM XML-SIZE GIVING XML-END
MOVE CXML-TAG-VALUE(2:XML-END) TO CXML-TEMP
MOVE CXML-TEMP TO CXML-TAG-VALUE
ELSE
INSPECT CXML-TAG-VALUE
TALLYING XML-COUNT FOR TRAILING SPACE
SUBTRACT XML-COUNT FROM XML-SIZE GIVING XML-END
IF XML-END XML-SIZE
MOVE 1 TO XML-END
END-IF
STRING CXML-TAG-VALUE(1:XML-END) DELIMITED BY SIZE
INTO XML-REC
WITH POINTER XML-PTR
MOVE SPACE TO CXML-TAG-VALUE.
IF CXML-TAG-VALUE NOT = SPACE
GO TO CXML-QUOTE.
CXML-QUOTE-EXIT.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Inspect Replacing

Phayes,
I haven't tested this code, but I wrote this real quick as perhaps a shorter way to do the same thing:


CXML-QUOTE.
    |Set end point of string.
    INSPECT CXML-TAG-VALUE
        TALLYING XML-COUNT FOR TRAILING SPACE.
    SUBTRACT XML-COUNT FROM XML-SIZE GIVING XML-END.
    |If end point zero, string is full size of variable.
    IF XML-END
        MOVE XML-COUNT TO XML-END.
    |A character at a time, construct a new string with
    |a combination of substitue characters and existing
    |characters.
    MOVE 1 TO XML-PTR.
    PERFORM VARYING XML-COUNT FROM 1 BY 1 UNTIL
        XML-COUNT > XML-END
        EVALUATE CXML-TAG-VALUE(XML-COUNT:1)
            WHEN "
                        INTO XML-REC WITH POINTER XML-PTR
            WHEN ">" STRING ">" DELIMITED BY SIZE
                        INTO XML-REC WITH POINTER XML-PTR
            WHEN "@" STRING "&at;" DELIMITED BY SIZE
                        INTO XML-REC WITH POINTER XML-PTR
            WHEN "`" WHEN "'"
                     STRING "'" DELIMITED BY SIZE
                        INTO XML-REC WITH POINTER XML-PTR
            WHEN OTHER STRING CXML-TAG-VALUE(XML-COUNT:1)
                           DELIMITED BY SIZE
                           INTO XML-REC WITH POINTER XML-PTR
        END-EVALUATE
    END-PERFORM.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Inspect Replacing

Hi DanM and phayes

Thanks for your solutions. I have implemented the one from phayes ? and it works perfectly.

The solution from DanM is easier to read and understand, but I also think that the solution from phayes is faster.

But then again - I don?t know J

Regards
Kim
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Inspect Replacing

Beginning of my example same as phayes:


IF CXML-TAG-VALUE NOT = SPACE
   SET XML-SIZE TO SIZE OF CXML-TAG-VALUE
   MOVE 0 TO XML-COUNT
   INSPECT CXML-TAG-VALUE TALLYING XML-COUNT
      FOR ALL "" "`" "'" "@"
   IF XML-COUNT > 0
      PERFORM CXML-QUOTE.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.