Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

INDEX() or POS() string functions in Enterprise COBOL?

295 views
Skip to first unread message

Farley, Peter x23353

unread,
Apr 4, 2008, 9:59:04 PM4/4/08
to
I have been looking around, but there doesn't seem to be an Enterprise
COBOL equivalent of the INDEX() or POS() string function anywhere
accessible from Enterprise COBOL. There isn't an INTRINSIC function for
it and there isn't an LE-provided function for it either. The INSPECT
verb only scans for individual characters, so that's no help.

Is this a case of just having to provide my own LE-enabled HLASM
implementation, or could I pull something quick-and-dirty like using the
C-library version of the index function?

Business need: Scan an input string value for "contains" a given
argument string anywhere inside the input string. Invariant: Length of
argument string always <= length of input string.

In REXX, I would say:

IF (POS(needle,haystack) > 0) ; THEN (Found it, do whatever is needed)
ELSE (Not there, do whatever business logic requires when absent)

where "needle" is the argument string and "haystack" is the input
string.

I've dl'd the latest CBT and COV indexes to see if this particular wheel
has already been invented and contributed, but it doesn't look like it
on first glance. Still looking though, I'll post back if I find
anything.

TIA for any info/url/RTFM you can provide.

Peter
This message and any attachments are intended only for the use of the addressee and
may contain information that is privileged and confidential. If the reader of the
message is not the intended recipient or an authorized representative of the
intended recipient, you are hereby notified that any dissemination of this
communication is strictly prohibited. If you have received this communication in
error, please notify us immediately by e-mail and delete the message and any
attachments from your system.

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to list...@bama.ua.edu with the message: GET IBM-MAIN INFO
Search the archives at http://bama.ua.edu/archives/ibm-main.html

Steve Comstock

unread,
Apr 4, 2008, 10:14:14 PM4/4/08
to
Farley, Peter x23353 wrote:
> I have been looking around, but there doesn't seem to be an Enterprise
> COBOL equivalent of the INDEX() or POS() string function anywhere
> accessible from Enterprise COBOL. There isn't an INTRINSIC function for
> it and there isn't an LE-provided function for it either. The INSPECT
> verb only scans for individual characters, so that's no help.

Where did you get that idea?

How about:

inspect trans-image tallying sel-ptr for characters
before 'SELECT '
inspect trans-image tallying spa-ptr for all ' '
before 'SELECT '
inspect trans-image tallying art-ptr for characters
before 'ARTIST='
inspect trans-image tallying tme-ptr for characters
before 'TIME='

That is, if you are looking for a string, inspect and
count the characters before the string; if the value is
0, the string is at the front. if the value equals the
size of your haystack, the string is not there.

More generally, perhaps:

inspect haystack tallying loc-ctr for characters before needle

You could also do:

inspect haystack tallying occ-ctr for all needle

to count the number of occurrences, which may be closer
to what you're after.


<ad>
String handling, and a whole lot more, are discussed
in our three-day class "Advanced Topics in COBOL".

Details at:

http://www.trainersfriend.com/COBOL_Courses/D725descrpt.htm

</ad>


>
> Is this a case of just having to provide my own LE-enabled HLASM
> implementation, or could I pull something quick-and-dirty like using the
> C-library version of the index function?
>
> Business need: Scan an input string value for "contains" a given
> argument string anywhere inside the input string. Invariant: Length of
> argument string always <= length of input string.
>
> In REXX, I would say:
>
> IF (POS(needle,haystack) > 0) ; THEN (Found it, do whatever is needed)
> ELSE (Not there, do whatever business logic requires when absent)
>
> where "needle" is the argument string and "haystack" is the input
> string.
>
> I've dl'd the latest CBT and COV indexes to see if this particular wheel
> has already been invented and contributed, but it doesn't look like it
> on first glance. Still looking though, I'll post back if I find
> anything.
>
> TIA for any info/url/RTFM you can provide.
>


Kind regards,

-Steve Comstock
The Trainer's Friend, Inc.

303-393-8716
http://www.trainersfriend.com

z/OS Application development made easier
* Our classes include
+ How things work
+ Programming examples with realistic applications
+ Starter / skeleton code
+ Complete working programs
+ Useful utilities and subroutines
+ Tips and techniques

==> call or email to receive a free sample student handout <==

Don Leahy

unread,
Apr 4, 2008, 10:17:13 PM4/4/08
to
Actually, INSPECT is not limited to single characters.

INSPECT WS-HAYSTACK
TALLYING WS-CHAR-TOTAL
FOR CHARACTERS
BEFORE INITIAL
WS-NEEDLE
IF WS-CHAR-TOTAL < LENGTH OF WS-HAYSTACK
....then you have a hit
ELSE
..... no hit.
END-IF

WS-NEEDLE can be any size. Often, it is expressed as:

WS-NEEDLE(1:ws-last-nonblank-char)

....where ws-last-nonblank-char is, as the name implies, the position
of the last nonblank character in WS-NEEDLE.

Steve Comstock

unread,
Apr 4, 2008, 10:31:06 PM4/4/08
to
Modifying my own response, and top-posting: you probably want my
first suggestion, not the second. Go with

inspect haystack tallying loc-ctr for characters before needle

CICS Guy

unread,
Apr 4, 2008, 10:36:51 PM4/4/08
to
I've always resorted to an inline perform loop with reference modification compares...

-----Original Message-----
From: IBM Mainframe Discussion List [mailto:IBM-...@BAMA.UA.EDU] On Behalf Of Farley, Peter x23353
Sent: Friday, April 04, 2008 2:59 PM
To: IBM-...@BAMA.UA.EDU
Subject: INDEX() or POS() string functions in Enterprise COBOL?
snip
Business need: Scan an input string value for "contains" a given argument string anywhere inside the input string. Invariant: Length of argument string always <= length of input string.
In REXX, I would say:
IF (POS(needle,haystack) > 0) ; THEN (Found it, do whatever is needed) ELSE (Not there, do whatever business logic requires when absent)
where "needle" is the argument string and "haystack" is the input string.

_________________________________________________________________
Get in touch in an instant. Get Windows Live Messenger now.
http://www.windowslive.com/messenger/overview.html?ocid=TXT_TAGLM_WL_Refresh_getintouch_042008

Farley, Peter x23353

unread,
Apr 6, 2008, 4:47:11 AM4/6/08
to
-----Original Message-----
From: IBM Mainframe Discussion List [mailto:IBM-...@BAMA.UA.EDU] On
Behalf Of Steve Comstock
Sent: Friday, April 04, 2008 6:13 PM
To: IBM-...@BAMA.UA.EDU
Subject: Re: INDEX() or POS() string functions in Enterprise COBOL?
<Snipped>
Where did you get that idea?
<Snipped>
More generally, perhaps:

inspect haystack tallying loc-ctr for characters before needle

<Unsnip>

Well, it really is true you learn something new every day. I did not
realize that the BEFORE/AFTER could be more than one character.

Thank you very much!

Peter

P.S. -- I truly do wish I could take your classes. Unfortunately I'm
not in charge of budgets.


This message and any attachments are intended only for the use of the addressee and
may contain information that is privileged and confidential. If the reader of the
message is not the intended recipient or an authorized representative of the
intended recipient, you are hereby notified that any dissemination of this
communication is strictly prohibited. If you have received this communication in
error, please notify us immediately by e-mail and delete the message and any
attachments from your system.

----------------------------------------------------------------------

Farley, Peter x23353

unread,
Apr 6, 2008, 4:50:32 AM4/6/08
to
-----Original Message-----
From: IBM Mainframe Discussion List [mailto:IBM-...@BAMA.UA.EDU] On
Behalf Of Don Leahy
Sent: Friday, April 04, 2008 6:17 PM
To: IBM-...@BAMA.UA.EDU
Subject: Re: INDEX() or POS() string functions in Enterprise COBOL?

Actually, INSPECT is not limited to single characters.

<Snipped>

Which I did not know and Steve also pointed out. Thank you also for
helping to cure my ignorance.

Farley, Peter x23353

unread,
Apr 6, 2008, 4:56:58 AM4/6/08
to
-----Original Message-----
From: IBM Mainframe Discussion List [mailto:IBM-...@BAMA.UA.EDU] On
Behalf Of CICS Guy
Sent: Friday, April 04, 2008 6:37 PM
To: IBM-...@BAMA.UA.EDU
Subject: Re: INDEX() or POS() string functions in Enterprise COBOL?

I've always resorted to an inline perform loop with reference
modification compares...

<Snipped>

Yes, that obviously would work, but I was looking for a less brute-force
solution. Hmm-m-m. I wonder which would be more efficient, for a range
of sizes of "needles"? INSPECT or inline performs?

Maybe I'll work that out if I can find any of those pesky round tuits
anywhere.

Thanks.

Peter


This message and any attachments are intended only for the use of the addressee and
may contain information that is privileged and confidential. If the reader of the
message is not the intended recipient or an authorized representative of the
intended recipient, you are hereby notified that any dissemination of this
communication is strictly prohibited. If you have received this communication in
error, please notify us immediately by e-mail and delete the message and any
attachments from your system.

----------------------------------------------------------------------

CICS Guy

unread,
Apr 6, 2008, 7:54:26 AM4/6/08
to
I found that on multiple character compares, when the string is first checked for the first single character first and if true then the full search text, it usually goes quite quickly - CLI for all the non matches and then CLC for the match......

-----Original Message-----
From: IBM Mainframe Discussion List [mailto:IBM-...@BAMA.UA.EDU] On Behalf Of Farley, Peter x23353
Sent: Saturday, April 05, 2008 9:57 PM
To: IBM-...@BAMA.UA.EDU
Subject: Re: INDEX() or POS() string functions in Enterprise COBOL?

-----Original Message-----
From: IBM Mainframe Discussion List [mailto:IBM-...@BAMA.UA.EDU] On Behalf Of CICS Guy
Sent: Friday, April 04, 2008 6:37 PM
To: IBM-...@BAMA.UA.EDU
Subject: Re: INDEX() or POS() string functions in Enterprise COBOL?
I've always resorted to an inline perform loop with reference modification compares... <Snipped>
Yes, that obviously would work, but I was looking for a less brute-force solution. Hmm-m-m. I wonder which would be more efficient, for a range of sizes of "needles"? INSPECT or inline performs?
snip
_________________________________________________________________
Use video conversation to talk face-to-face with Windows Live Messenger.
http://www.windowslive.com/messenger/connect_your_way.html?ocid=TXT_TAGLM_WL_Refresh_messenger_video_042008

Paul Gilmartin

unread,
Apr 6, 2008, 12:57:55 PM4/6/08
to
On Fri, 4 Apr 2008 17:58:36 -0400, Farley, Peter x23353 wrote:

>I have been looking around, but there doesn't seem to be an Enterprise
>COBOL equivalent of the INDEX() or POS() string function anywhere
>

>TIA for any info/url/RTFM you can provide.
>

Boyer-Moore?

Linkname: Boyer-Moore string search algorithm - Wikipedia, the free encyclopedia
URL: http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm

... or is that too PFCSK for a COBOL programmer?

-- gil

Don Leahy

unread,
Apr 6, 2008, 6:33:54 PM4/6/08
to
On Sun, Apr 6, 2008 at 8:57 AM, Paul Gilmartin <PaulGB...@aim.com> wrote:
> On Fri, 4 Apr 2008 17:58:36 -0400, Farley, Peter x23353 wrote:
>
> >I have been looking around, but there doesn't seem to be an Enterprise
> >COBOL equivalent of the INDEX() or POS() string function anywhere
> >
>
> >TIA for any info/url/RTFM you can provide.
> >
> Boyer-Moore?
>
> Linkname: Boyer-Moore string search algorithm - Wikipedia, the free encyclopedia
> URL: http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
>
> ... or is that too PFCSK for a COBOL programmer?
>
> -- gil
>
No, that is too much *work* for a COBOL programmer when the COBOL
compiler provides a perfectly reasonable way to achieve the result.
:-)

Besides, for all I know the INSPECT verb may use Boyer-Moore under the covers.

Farley, Peter x23353

unread,
Apr 7, 2008, 2:58:53 PM4/7/08
to
> -----Original Message-----
> From: IBM Mainframe Discussion List [mailto:IBM-...@BAMA.UA.EDU] On
> Behalf Of Paul Gilmartin
> Sent: Sunday, April 06, 2008 8:58 AM
> To: IBM-...@BAMA.UA.EDU
> Subject: Re: INDEX() or POS() string functions in Enterprise COBOL?
<Snipped>
> Boyer-Moore?
>
> Linkname: Boyer-Moore string search algorithm - Wikipedia, the free
> encyclopedia
> URL:
>
http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
>
> ... or is that too PFCSK for a COBOL programmer?

Sophisticated, efficient -- I love it. Thanks for the reference, that
one goes into my keep list.

But as Don said, that's *way* too much work when INSPECT provides what I
need, as do the index() or pos() function in other languages.

Yes, I do understand and solve problems in more then one (computer)
language. Programmers who are fluent in COBOL are not limited to that
language skill, they are only limited by what business will pay them to
support.

Thanks again for the interesting reference.

Peter
This message and any attachments are intended only for the use of the addressee and
may contain information that is privileged and confidential. If the reader of the
message is not the intended recipient or an authorized representative of the
intended recipient, you are hereby notified that any dissemination of this
communication is strictly prohibited. If you have received this communication in
error, please notify us immediately by e-mail and delete the message and any
attachments from your system.

----------------------------------------------------------------------

Don Leahy

unread,
Apr 7, 2008, 4:05:58 PM4/7/08
to
On Mon, Apr 7, 2008 at 10:58 AM, Farley, Peter x23353
<Peter....@broadridge.com> wrote:
<SNIP>

>
. Programmers who are fluent in COBOL are not limited to that
> language skill, they are only limited by what business will pay them to
> support.
>

Exactly. In many shops, a COBOL programmer who coded a Boyer-Moore
algorithm would be in serious trouble with his project lead. :-)

0 new messages