I'm not sure what you want to achieve, but to compare two fields
character-by-character you could use the following code:
(sample definitions)
D FIELD1 S 999
D FIELD2 S 999
D LENGTH S 3 0 INZ(%SIZE(FIELD1))
D INDEX S + 2 LIKE(LENGTH)
C 1 DO LENGTH INDEX
C IF %SUBST(FIELD1:INDEX:1) =
C %SUBST(FIELD2:INDEX:1)
...
(your code - you might want to leave the loop using the LEAVE op-code)
...
C ENDIF
C ENDDO
Why can't you just compare them?
IF Field1=Field2?
If Field1 is 7 char long and Field2 is 8 char long, RPG will test with the
greatest length padded with blanks so you will test :
"America " (with a blank) = "American" and that's false.
See http://publib.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/QB3AGZ00/4.3.6.
How about
If Trim(Field1) = Substr(Field2,Len(Field1)) Then
Shouldn't it be
If Trim(Field1) = Substr(Field2,Len(trim(Field1))) ??
But they don't match... with or without the blank...so I don't see the
point.
Unless you want
If Str1 = %SubSt(Str2:1:%Len(%Trim(Str1))
you could use the c function strcmp
D strcmp PR 10I 0 ExtProc('strncmp')
D s1 1000 Value
D s2 1000 Value
To use this you would have to terminate the string with nulls
C Eval rc = strcmp(s1+x'00':s2+x'00)
But using strcmp gives the exact same answer as comparing the strings in
RPG, only in a slower and more complicated way.
By the way, Mike, your prototype for strcmp won't work.
> D strcmp PR 10I 0 ExtProc('strncmp')
> D s1 1000 Value
> D s2 1000 Value
It should be one of these (two ways to fix the parameters + two
different functions (strcmp takes 2 parms, strncmp takes 3)
D strcmp PR 10I 0 ExtProc('strcmp')
D s1 1000 Const
D s2 1000 Const
D strcmp PR 10I 0 ExtProc('strcmp')
D s1 * Value options(*string)
D s2 * Value options(*string)
OPTIONS(*STRING) isn't strictly necessary.
D strcmp PR 10I 0 ExtProc('strncmp')
D s1 1000 Const
D s2 1000 Const
D len 10u 0 Value
D strcmp PR 10I 0 ExtProc('strncmp')
D s1 * Value options(*string)
D s2 * Value options(*string)
D len 10u 0 Value
Barbara Morris