Finding duplicate in variable lenght string in cobol



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Finding duplicate in variable lenght string in cobol

Postby kanipriya2785 » Fri Jun 17, 2022 10:02 pm

Hi,
How to find duplicate in variable length string in cobol.

Ex., ABCDEEFGGHII

I need to find duplicates in above string.
kanipriya2785
 
Posts: 7
Joined: Fri Jun 17, 2022 9:59 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Finding duplicate in variable lenght string in cobol

Postby Robert Sample » Sat Jun 18, 2022 5:31 am

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?
Robert Sample
Global moderator
 
Posts: 3686
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 274 times

Re: Finding duplicate in variable lenght string in cobol

Postby kanipriya2785 » Tue Jun 21, 2022 2:02 am

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
kanipriya2785
 
Posts: 7
Joined: Fri Jun 17, 2022 9:59 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Finding duplicate in variable lenght string in cobol

Postby Robert Sample » Tue Jun 21, 2022 7:49 pm

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.
Robert Sample
Global moderator
 
Posts: 3686
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 274 times

Re: Finding duplicate in variable lenght string in cobol

Postby kanipriya2785 » Tue Jun 21, 2022 9:20 pm

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.
kanipriya2785
 
Posts: 7
Joined: Fri Jun 17, 2022 9:59 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Finding duplicate in variable lenght string in cobol

Postby enrico-sorichetti » Tue Jun 21, 2022 9:40 pm

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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2978
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 160 times

Re: Finding duplicate in variable lenght string in cobol

Postby kanipriya2785 » Tue Jun 21, 2022 9:45 pm

ok. I will try. Thank you.
kanipriya2785
 
Posts: 7
Joined: Fri Jun 17, 2022 9:59 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Finding duplicate in variable lenght string in cobol

Postby enrico-sorichetti » Tue Jun 21, 2022 11:28 pm

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<
 
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2978
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 160 times

Re: Finding duplicate in variable lenght string in cobol

Postby sergeyken » Wed Jun 22, 2022 3:10 am

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…
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 285
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 5 times
Been thanked: 25 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post