Search a variable and find for strings in cobol

This is a Mainframe COBOL forum - you can post your queries on Mainframe COBOL, VS COBOL II, COBOL/370 , Enterprise COBOL

Moderators: dbzTHEdinosauer, Moderator Group

Post Reply
Arun.Balu
Member
Posts: 5
Joined: Fri Jun 29, 2012 8:33 pm

Search a variable and find for strings in cobol

Post by Arun.Balu » Mon Jul 16, 2012 7:04 pm

Search a variable and find for strings in cobol

The string size can varies

Fore.g

I have to set a switch when any of the below string are found in the variable
1)CAT
2)MOUSE
3)ELEPHANT
4)AEROPLANE

Please note that these strings varies with size.

And i want these strings defined in working storage area NOT inside procedure division(Coding standard, sine there might be a later enhancement to add in more strings

User avatar
dbzTHEdinosauer
Moderator
Posts: 981
Joined: Mon Oct 02, 2006 8:31 pm

Post by dbzTHEdinosauer » Mon Jul 16, 2012 7:30 pm

so, what have you in mind?

do you know how you want to define your working-storage?
do you know what COBOL Statements you are going to employ?
the COBOL manuals (Programming Guide and Reference) are found by the IBM MANUALS link at the top of the page.

Sorry,
we only help,
we don't do it for you.
Dick Brenholtz
JCL, SQL and code in programs have an irritating habit of doing what you say,
not what you meant.

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Mon Jul 16, 2012 11:31 pm

Suggest you map out how you intend to do this (using pseudo-code) and post your approach here. We can review your approach.

No sense trying to implement code until the approach has been reviewed.
Have a good one

Arun.Balu
Member
Posts: 5
Joined: Fri Jun 29, 2012 8:33 pm

Post by Arun.Balu » Tue Jul 17, 2012 7:55 am

Okay..

Here is the list of code which i tried..

1) Binary search
2) Linear search

Both search command works only if the string we are searching is of same length.. For e.g. If i have to search CAT and ELEPHANT in a variable, i have to declare a Temporary variable which stores those strings.. 'CAT ' CAT appended with spaces to match string length of elephant..

3) INSPECT command

NSPECT WSANIMAL-NAME
TALLYING NAME-1 FOR ALL 'CAT'
NAME-2 FOR ALL 'ELEPHANT'

This logic needs those strings to be hardcoded inside the procedure division..

But i do want these strings to be declared in working storage area..

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Tue Jul 17, 2012 8:31 am

Keep in mind that COBOL doesn't have strings. . .

You haven't really posted anythng like the requested pseudo-code.

If the lenght of the data to be searched varies, you will need to have a way to specify the length for the current search. You will also need to identify the length of each of the values you want to look for.

One way to do what you want would be to use reference modification to compare the animal values against the "variable". If you have never done something like this, it will probably take a while to get the hang of it.

Basically, you would write code to "parse" the variable looking for the value(s) that would require setting "the switch".
Have a good one

William Collins
Active Member
Posts: 732
Joined: Thu May 24, 2012 4:07 am

Post by William Collins » Tue Jul 17, 2012 7:32 pm

The reference-modification can be a little "ungainly".

You can have variable-length fields, using Occurs Depending On. This will probably/possibly be a little "slower" than reference-modification, but the code will be much easier to understand.

What do you want to do when you "find" the data?

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Wed Jul 18, 2012 8:53 am

What do you want to do when you "find" the data?
Set the "switch" . . . :)

I'd not fall on my sword for either alternative. . . I suppose i'd go with whatever the shop standards permit and look for someone in the organization who has done somethng similar.

In the "old days" the way we wrote things like this was in Assembler 8)
Have a good one

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others