View previous topic :: View next topic
|
Author |
Message |
Soundammal.S
New User
Joined: 05 May 2008 Posts: 29 Location: Chennai
|
|
|
|
Hi All,
I have coded a REXX to extract all the lines between two particular words. The input is the COBOL program and the output should be copied to PS.
Please find the code below:
Code: |
/*REXX*/
"EXECIO * DISKR INPUT (FINIS STEM LINE."
START="EXEC"
EN="END-EXEC."
J=1
DO I=1 TO LINE.0 BY 1
HLQ=LINE.I
TEMP=STRIP( ''HLQ'','B')
PARSE VAR TEMP HLQT " " APPT
IF COMPARE(''HLQT'',''START'')=0 THEN DO
VAL=COMPARE(''HLQT'',''EN'')
DO WHILE VAL/=0
IF COMPARE(''HLQT'',''EN'')=0 THEN DO
VAL=0
END
NEW.J=LINE.I
J=J+1
I=I+1
HLQT=LINE.I
IF I>LINE.0 THEN DO
VAL=0
END
END
END
END
"EXECIO * DISKW MYOUTDD (STEM NEW. FINIS"
|
It extracts only first occurence of the keywords specified and displays all other lines. But I want the output such as,
EXEC
........
.......
END-EXEC.
EXEC
..........
END-EXEC.
Please help me to get the correct output. |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8797 Location: Welsh Wales
|
|
|
|
Use TRACE I to follow through and see what happens where to find the error(s) |
|
Back to top |
|
|
Soundammal.S
New User
Joined: 05 May 2008 Posts: 29 Location: Chennai
|
|
|
|
I tried with TRACE I but i didnt get any clue..Can you please help me out. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
why are you incrementing i ?
|
|
Back to top |
|
|
genesis786
Active User
Joined: 28 Sep 2005 Posts: 210 Location: St Katherine's Dock London
|
|
|
|
ADD
I=I-1 after DO WHILE VAL/=0 LOOP...
Code: |
DO WHILE VAL/=0
IF COMPARE(''HLQT'',''EN'')=0 THEN DO
VAL=0
END
NEW.J=LINE.I
SAY SPACE(NEW.J)
J=J+1
I=I+1
HLQT=LINE.I
IF I>LINE.0 THEN DO
VAL=0
END
END
I=I-1
|
|
|
Back to top |
|
|
genesis786
Active User
Joined: 28 Sep 2005 Posts: 210 Location: St Katherine's Dock London
|
|
|
|
also..
you must have noted that, your logic is working fine for cases like:
EXEC
...
..
END-EXEC.
..
..
EXEC
...
...
END-EXEC.
but not when it is:
EXEC
..
..
END-EXEC.
EXEC
.. <<<--- variable 'I' pointing here after loopin in inner loop...
..
END-EXEC.
that's because your inner DO WHILE VAL/=0 loop increments I once after END-EXEC and once in our outer main loop, so it points to next line after EXEC.
hope this helps |
|
Back to top |
|
|
Pedro
Global Moderator
Joined: 01 Sep 2006 Posts: 2546 Location: Silicon Valley
|
|
|
|
I think it is a poor practice to increment a 'do' variable within the loop. Especially poor when there is no particular reason to do so.
I suggest using another variable name instead of 'I' here:
Code: |
DO WHILE VAL/=0
IF COMPARE(''HLQT'',''EN'')=0 THEN DO
VAL=0
END
NEW.J=LINE.I
SAY SPACE(NEW.J)
J=J+1
Q=I+1
HLQT=LINE.Q
IF Q>LINE.0 THEN DO
VAL=0
END
END |
|
|
Back to top |
|
|
genesis786
Active User
Joined: 28 Sep 2005 Posts: 210 Location: St Katherine's Dock London
|
|
|
|
another way:
Code: |
DO WHILE VAL/=0
IF COMPARE(''HLQT'',''EN'')=0 THEN DO
VAL=0
NEW.J=LINE.I
SAY SPACE(NEW.J)
J=J+1
LEAVE
END
NEW.J=LINE.I
SAY SPACE(NEW.J)
J=J+1
I=I+1
HLQT=LINE.I
IF I>LINE.0 THEN DO
VAL=0
END
END
|
|
|
Back to top |
|
|
Douglas Wilder
Active User
Joined: 28 Nov 2006 Posts: 305 Location: Deerfield IL
|
|
|
|
You have a very nice loop here
Code: |
DO I=1 TO LINE.0 BY 1 |
Why do you need another loop?
Just use the first loop, check to see if you need to print the line you are on and if so put it in the output stem. |
|
Back to top |
|
|
MBabu
Active User
Joined: 03 Aug 2008 Posts: 400 Location: Mumbai
|
|
|
|
I haven't traced the logic of this but I've never seen COMPARE(). Why not just compare the strings? Also, no need to concatenate empty strings ('') to the variables.
IF COMPARE(''HLQT'',''START'')=0 THEN
would be more efficient as
IF HLQT = START THEN
Also, this code does not appear to take into account the fact that the words can also appear in comments, be lower case, be on the same line eg: EXEC CICS RETURN END-EXEC, have sequence numbers in 1-6 or 73-80 or that the statement can end with a period. This code might work better
Code: |
/* REXX */
"EXECIO * DISKR INPUT (FINIS STEM LINE."
EXECSTMT = 0
TAIL=0
DO I = 1 TO LINE.0
PARSE UPPER VAR LINE.I . 7 LINE 73 .
LINE = TRANSLATE(LINE," ",".")
IF SUBSTR(LINE,1,1) = "*" THEN ITERATE
PARSE VAR LINE KEYWORD .
IF KEYWORD = "EXEC" & \EXECSTMT THEN EXECSTMT = 1
IF EXECSTMT THEN
DO
TAIL=TAIL+1
NEW.TAIL=LINE.I
END
IF WORDPOS("END-EXEC",LINE)>0 THEN EXECSTMT = 0
END
NEW.0=TAIL
"EXECIO * DISKW MYOUTDD (STEM NEW. FINIS" |
|
|
Back to top |
|
|
Soundammal.S
New User
Joined: 05 May 2008 Posts: 29 Location: Chennai
|
|
|
|
Thanks Babu. It is working fine. |
|
Back to top |
|
|
Soundammal.S
New User
Joined: 05 May 2008 Posts: 29 Location: Chennai
|
|
|
|
Hi All,
Now I'm able to extract the lines between EXEC and END-EXEC. My output is like this,
026300 EXEC SQL
026400 INCLUDE SQLCA
026500 END-EXEC.
Can I remove the line numbers using STRIP command? Please suggest. |
|
Back to top |
|
|
genesis786
Active User
Joined: 28 Sep 2005 Posts: 210 Location: St Katherine's Dock London
|
|
|
|
TRY..
Code: |
STRIP(TRANSLATE(WORD(LINE.I,1),' ','1234567890'),,' ')
|
|
|
Back to top |
|
|
MBabu
Active User
Joined: 03 Aug 2008 Posts: 400 Location: Mumbai
|
|
|
|
Soundammal.S wrote: |
Can I remove the line numbers using STRIP command? Please suggest. |
Yes, but that will shift over lines that have sequence numbers and not those that don't. my sample rexx was written so that you could print either the original line including sequence numbers or the line with no sequence numbers. just print LINE instead of LINE.I on line 14. Actually, you should create a new variable with parse as is done in line 6 since LINE will be upper case and have its periods removed, but you get the idea. |
|
Back to top |
|
|
Soundammal.S
New User
Joined: 05 May 2008 Posts: 29 Location: Chennai
|
|
|
|
Hi All,
The below code is working fine.
/* REXX */
"ALLOC F(INPUT) DA('MYHLQ.INPUTFIL') SHR "
"EXECIO * DISKR INPUT (FINIS STEM LINE."
"FREE F(INPUT)"
EXECSTMT = 0
TAIL=0
DO I = 1 TO LINE.0
PARSE UPPER VAR LINE.I . 10 LINE 73 .
LINE = TRANSLATE(LINE," ",".")
IF SUBSTR(LINE,1,1) = "*" THEN ITERATE
PARSE VAR LINE KEYWORD .
IF KEYWORD = "EXEC" & \EXECSTMT THEN EXECSTMT = 1
IF EXECSTMT THEN
DO
TAIL=TAIL+1
K=LINE.I
PARSE VAR K VAR1 " " VAR2
NEW.TAIL=VAR2
END
IF WORDPOS("END-EXEC",LINE)>0 THEN EXECSTMT = 0
END
NEW.0=TAIL
"DELETE 'T49NXSS.TEST'"
"ALLOC DA('MYHLQ.TEST') F(MYOUTDD) NEW REUSE"
"EXECIO * DISKW MYOUTDD (OPEN"
"EXECIO * DISKW MYOUTDD (STEM NEW. "
"EXECIO * DISKW MYOUTDD (FINIS"
"FREE F(MYOUTDD)"
Thank you all for your support. |
|
Back to top |
|
|
Mickeydusaor
Active User
Joined: 24 May 2006 Posts: 258 Location: Salem, Oregon
|
|
|
|
you do not need to have 3 EXECIO commands, 1 would do the same thing as the 3.
"EXECIO * DISKW MYOUTDD (STEM NEW. FINIS" |
|
Back to top |
|
|
MBabu
Active User
Joined: 03 Aug 2008 Posts: 400 Location: Mumbai
|
|
|
|
Also, I'm not sure what PARSE VAR K VAR1 " " VAR2 is supposed to do, though I assume it is supposed to remove the 1st word. The space is implied so having a quoted blank doesn't do anything and I'm not sure why you would want to remove the 1st word of each line. Also there is no need to assign var1 if you aren't going to use it so a period is more efficient, eg: parse var K . var2 |
|
Back to top |
|
|
|