Page 1 of 1

Finding duplicate in variable lenght string in cobol

PostPosted: Fri Jun 17, 2022 10:02 pm
by kanipriya2785
Hi,
How to find duplicate in variable length string in cobol.

Ex., ABCDEEFGGHII

I need to find duplicates in above string.

Re: Finding duplicate in variable lenght string in cobol

PostPosted: Sat Jun 18, 2022 5:31 am
by Robert Sample
First, you need to understand that there isn't really a variable length string in COBOL. Alphanumeric variables (PIC X) have a length that is set at the time the program is compiled, and that length cannot be varied without editing the source code and recompiling it.

Second, your post is woefully inadequate in describing your problem. These duplicates -- are they always side-by-side in the variable? What if you have ABCDABC -- duplicates or not? How long is the variable? Is there a limit of 1 duplicate or could you have something like ABCDDDDEFF? What do you want to do when you find a duplicate? Print it out? Store it in another variable? Delete the duplicates from the variable?

Re: Finding duplicate in variable lenght string in cobol

PostPosted: Tue Jun 21, 2022 2:02 am
by kanipriya2785
Sorry for the less details. My input file has only one field and that is string declared as X(15). I need to find is there any duplicate alphabet in the string.

If it is duplicate i need to write that records alone in output-duplicate file. Rest i will Write in normal output file.

My sample/test records as follows:
ABCDEEFGGHIIEDH
TODAYISBESTDAYA
MAINFRAMEFILES

Re: Finding duplicate in variable lenght string in cobol

PostPosted: Tue Jun 21, 2022 7:49 pm
by Robert Sample
Based on your post, all three of the sample strings you provided have duplicates since you did NOT specify that duplicates have to be next to each other.

Re: Finding duplicate in variable lenght string in cobol

PostPosted: Tue Jun 21, 2022 9:20 pm
by kanipriya2785
In my case duplicates can be anywhere in the string. As you said all three strings are duplicates. But I need a logic to figure out the string has duplicates in COBOL program. Once i figure out the string has duplicate, then i will write that record alone into my duplicate output file.

Appreciate your help.

Re: Finding duplicate in variable lenght string in cobol

PostPosted: Tue Jun 21, 2022 9:40 pm
by enrico-sorichetti
google with shortest/longest repeated substring

to get the algorithm and the code snippets in python, java , c

you will have to port it to cobol yourself

Re: Finding duplicate in variable lenght string in cobol

PostPosted: Tue Jun 21, 2022 9:45 pm
by kanipriya2785
ok. I will try. Thank you.

Re: Finding duplicate in variable lenght string in cobol

PostPosted: Tue Jun 21, 2022 11:28 pm
by enrico-sorichetti
the suggestion posted was for an academically correct full approach

since you only need to know if there are duplicates and not determine their value
a simple scan for repeated chars would be more than enough

here is a smal proof of concept written in rexx

the script
trace "O"

strings.1 = "ABCDEEFGGHIIEDH"
strings.2 = "TODAYISBESTDAYA"
strings.3 = "MAINFRAMEFILES"
strings.4 = "noduplicates"
strings.0 = 4

do s = 1 to strings.0
  say "checking >"strings.s"<"
  do i = 1 to length(strings.s) - 1
    c = substr(strings.s,i,1 )
    do j = i+1 to length(strings.s)
      if substr(strings.s,j,1 ) = c then do
        say " match found for >"strings.s"<" "at" i j ">"c"<"
        iterate s
      end
    end
  end
  say " no duplicates in >"strings.s"<"
end
 


the result
~/z % rexx dups                                                          
checking >ABCDEEFGGHIIEDH<
 match found for >ABCDEEFGGHIIEDH< at 4 14 >D<
checking >TODAYISBESTDAYA<
 match found for >TODAYISBESTDAYA< at 1 11 >T<
checking >MAINFRAMEFILES<
 match found for >MAINFRAMEFILES< at 1 8 >M<
checking >noduplicates<
 no duplicates in >noduplicates<
 

Re: Finding duplicate in variable lenght string in cobol

PostPosted: Wed Jun 22, 2022 3:10 am
by sergeyken
kanipriya2785 wrote:Hi,
How to find duplicate in variable length string in cobol.

Ex., ABCDEEFGGHII

I need to find duplicates in above string.

Try to learn at least something about string processing in COBOL. The statements like: INSPECT, UNSTRING, STRING…