trouble with INSPECT 
Author Message
 trouble with INSPECT

Hi all:

I am trying to use INSPECT to count the number of alphabetic characters in
a variable. I do not want to count spaces.

The other thing I am doing is converting from lower-case to upper. That
part seems to work ok.

Can anyone see what I'm doing wrong with the second INSPECT? Or am I
trying to do something that can't be done with INSPECT and I need to
approach it in a different way?

Here is my code:

01  KEY-CNT                PIC 99 VALUE 0.
01  WS-COMM-NAME           PIC X(30) VALUE SPACES.
01  WS-LOWER               PIC X(26) VALUE 'abcdefghijklmnopqrstuvwxyz'.
01  WS-UPPER               PIC X(26) VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

INITIALIZE KEY-CNT.
INSPECT WS-COMM-NAME CONVERTING WS-LOWER TO WS-UPPER.
INSPECT WS-COMM-NAME TALLYING KEY-CNT FOR ALL WS-UPPER.

WS-COMM-NAME can have anywhere from 1 to 30 alphabetic characters in it.

Thanks in advance.


Melinda

--
Melinda Belleville
University of Kentucky                    JJG
217 Mcvey Hall                  Aug. 1, 1942 - Aug. 9, 1995
Lexington, Ky  40506          



Mon, 25 Sep 2000 03:00:00 GMT  
 trouble with INSPECT

The second INSPECT is counting occurrences of a 26-byte string, not
occurrences of each of 26 bytes.  How about this ?

01 WS-COMM-NAME2    PIC X(30).
01 WS-HIGH-VAL            PIC  X(26) VALUE HIGH-VALUES.

Instead of your 2nd INSPECT, do:

MOVE WS-COMM-NAME TO WS-COMM-NAME2.
INSPECT WS-COMM-NAME2 CONVERTING WS-UPPER TO WS-HIGH-VAL.
INSPECT WS-COMM-NAME2 TALLING KEY-CNT FOR ALL HIGH-VALUE.

Quote:

> Hi all:

> I am trying to use INSPECT to count the number of alphabetic characters in
> a variable. I do not want to count spaces.

> The other thing I am doing is converting from lower-case to upper. That
> part seems to work ok.

> Can anyone see what I'm doing wrong with the second INSPECT? Or am I
> trying to do something that can't be done with INSPECT and I need to
> approach it in a different way?

> Here is my code:

> 01  KEY-CNT                PIC 99 VALUE 0.
> 01  WS-COMM-NAME           PIC X(30) VALUE SPACES.
> 01  WS-LOWER               PIC X(26) VALUE 'abcdefghijklmnopqrstuvwxyz'.
> 01  WS-UPPER               PIC X(26) VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

> INITIALIZE KEY-CNT.
> INSPECT WS-COMM-NAME CONVERTING WS-LOWER TO WS-UPPER.
> INSPECT WS-COMM-NAME TALLYING KEY-CNT FOR ALL WS-UPPER.

> WS-COMM-NAME can have anywhere from 1 to 30 alphabetic characters in it.

> Thanks in advance.


> Melinda

> --
> Melinda Belleville
> University of Kentucky                    JJG
> 217 Mcvey Hall                  Aug. 1, 1942 - Aug. 9, 1995
> Lexington, Ky  40506




Mon, 25 Sep 2000 03:00:00 GMT  
 trouble with INSPECT

But if WS-COMM-NAME happens to contain one or more HIGH-VALUES
your count is wrong! :-(

We could add another INSPECT which converts HIGH-VALUE to SPACE
before your suggested code. That should assure a correct result.

Not bad! Four INSPECTS in a row. Is that a record?

Rick Smith
--------------------------------------------------------

Quote:

>The second INSPECT is counting occurrences of a 26-byte string, not
>occurrences of each of 26 bytes.  How about this ?

>01 WS-COMM-NAME2    PIC X(30).
>01 WS-HIGH-VAL            PIC  X(26) VALUE HIGH-VALUES.

>Instead of your 2nd INSPECT, do:

>MOVE WS-COMM-NAME TO WS-COMM-NAME2.
>INSPECT WS-COMM-NAME2 CONVERTING WS-UPPER TO WS-HIGH-VAL.
>INSPECT WS-COMM-NAME2 TALLING KEY-CNT FOR ALL HIGH-VALUE.


>> Hi all:

>> I am trying to use INSPECT to count the number of alphabetic characters
in
>> a variable. I do not want to count spaces.

>>[snipped]

>> Here is my code:

>> 01  KEY-CNT                PIC 99 VALUE 0.
>> 01  WS-COMM-NAME           PIC X(30) VALUE SPACES.
>> 01  WS-LOWER               PIC X(26) VALUE 'abcdefghijklmnopqrstuvwxyz'.
>> 01  WS-UPPER               PIC X(26) VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

>> INITIALIZE KEY-CNT.
>> INSPECT WS-COMM-NAME CONVERTING WS-LOWER TO WS-UPPER.
>> INSPECT WS-COMM-NAME TALLYING KEY-CNT FOR ALL WS-UPPER.

>> WS-COMM-NAME can have anywhere from 1 to 30 alphabetic characters in it.

>> Thanks in advance.


>> Melinda

>> --
>> Melinda Belleville
>> University of Kentucky                    JJG
>> 217 Mcvey Hall                  Aug. 1, 1942 - Aug. 9, 1995
>> Lexington, Ky  40506




Mon, 25 Sep 2000 03:00:00 GMT  
 trouble with INSPECT

Quote:

> But if WS-COMM-NAME happens to contain one or more HIGH-VALUES
> your count is wrong! :-(

> We could add another INSPECT which converts HIGH-VALUE to SPACE
> before your suggested code. That should assure a correct result.

> Not bad! Four INSPECTS in a row. Is that a record?

For inefficiency.

How about this:

01 WS-COMM-NAME2    PIC X(30).
01  WS-ALPHA         PIC  X(53) VALUE
        "*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
01  WS-STARS         PIC  X(53) VALUE
        " ****************************************************".

MOVE WS-COMM-NAME TO WS-COMM-NAME2
INSPECT WS-COMM-NAME2 CONVERTING WS-ALPHA TO WS-HIGH-STARS
INSPECT WS-COMM-NAME2 TALLING KEY-CNT FOR ALL "*"

Two inspects rather than four.  Unless converting lower case to upper
was also part of the exercise.

I used "*" rather than HIGH-VALUES because it's easier to use in
literals.

--
I  |\   Randall Bart

o  |\   1-310-542-6013                       Please reply without spam
v  | \  
e    |\ Todd McCormick jailed for using Marinol with prescription:
Y    |/        http://{*filter*}magazine.com/toc/articles/toddheld.html
o    |\ Panic in the Year Zero Zero:   http://www.*-*-*.com/ ;
u    |/ Is it easy yet?: http://www.*-*-*.com/



Mon, 25 Sep 2000 03:00:00 GMT  
 trouble with INSPECT

Quote:


> > But if WS-COMM-NAME happens to contain one or more HIGH-VALUES
> > your count is wrong! :-(
[snip]
> For inefficiency.

> How about this:

> 01 WS-COMM-NAME2    PIC X(30).
> 01  WS-ALPHA         PIC  X(53) VALUE
>         "*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
> 01  WS-STARS         PIC  X(53) VALUE
>         " ****************************************************".

> MOVE WS-COMM-NAME TO WS-COMM-NAME2
> INSPECT WS-COMM-NAME2 CONVERTING WS-ALPHA TO WS-HIGH-STARS
> INSPECT WS-COMM-NAME2 TALLING KEY-CNT FOR ALL "*"

> Two inspects rather than four.  Unless converting lower case to upper
> was also part of the exercise.

> I used "*" rather than HIGH-VALUES because it's easier to use in
> literals.

[snip]

The following suggestion is offered in the spirit of good humor, and as
a mild warning about 'efficiency':

01 WS-COMM-NAME2     PIC X(30).

MOVE WS-COMM-NAME TO WS-COMM-NAME2
INSPECT WS-COMM-NAME2 CONVERTING    
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    TO ALL "A".
INSPECT WS-COMM-NAME2 TALLYING KEY-CNT FOR ALL "A"
(don't forget to initialize KEY-CNT)

Use one of the characters you are interested in and you avoid Rick
Smith's problem (HIGH-VALUES is not an alphabetic character) in a
technique that is, perhaps, less obscure.  The use of literals, rather
than data-items, for the CONVERTING operands, makes it easier for your
compiler to generate faster translations, since the compiler may assume
that the CONVERTING operands are not going to change at run time.

This technique, combined with reference modification, can be quite
useful in many applications where there is a need to find clusters of
similar characters (alphabetic, numeric, etc).

--

Liant Software Corporation   http://www.liant.com/
512-343-1010  FAX:512-343-9487



Tue, 26 Sep 2000 03:00:00 GMT  
 trouble with INSPECT

Quote:



>> > But if WS-COMM-NAME happens to contain one or more HIGH-VALUES
>> > your count is wrong! :-(

[snip]

This was a school problem courtesy of Melinda Belleville
at the University of Kentucky.

1. The problem was to use the INSPECT statement to count alphabetic
    characters in a variable, excluding spaces.
2. The variable can have from 1 to 30 letters or spaces.
3. Also convert lower case to upper.

The solutions given, other than Ms. Belleville's solution, for which she
requested
assistance, work -- if the data meets condition 2.

My objection with using HIGH-VALUES was a remark on the unknown quality
of the data. If the quality of the data is questionable, the count WILL be
wrong
if the data contains the suggested single character HIGH-VALUE or "*" or
any non-capital letter.

The original code segements were:

01  KEY-CNT                PIC 99 VALUE 0.
01  WS-COMM-NAME           PIC X(30) VALUE SPACES.
01  WS-LOWER               PIC X(26) VALUE 'abcdefghijklmnopqrstuvwxyz'.
01  WS-UPPER               PIC X(26) VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

INITIALIZE KEY-CNT.
INSPECT WS-COMM-NAME CONVERTING WS-LOWER TO WS-UPPER.
INSPECT WS-COMM-NAME TALLYING KEY-CNT FOR ALL WS-UPPER.

I replied to Ms. Belleville, directly, with the suggestion of replacing the
second
INSPECT with the following:

INSPECT WS-COMM-NAME TALLYING KEY-CNT
    FOR ALL "A" "B" "C" . . . "Z".

This is a minimal solution to the problem. It uses INSPECT and gives the
correct
count; but it still does not address the quality of the data.

A more complete solution would be:

IF WS-COMM-NAME IS ALPHABETIC
    INTIALIZE . . . *> original
    INSPECT . . . CONVERTING . . . *> original
    INSPECT . . . TALLYING . . . *> suggested
    IF KEY-CNT < 1
        *> Error: does not meet the requirement for 1 to 30 alphabetic,
non-space
    END-IF
ELSE
    *> Error: contains non-alphabetic characters
END-IF

Using a PERFORM with reference modification to test the individual
characters
might (probably would) be more efficient. It depends on the compiler. But it
doesn't use INSPECT!

I might also argue that a CALL to a language which uses toupper()
and isupper() might improve the efficiency -- but I won't! :-)
Such a solution would violate the written condition to use INSPECT
and the implied condition to use COBOL.

Rick Smith
-----------------------------------------------------



Tue, 26 Sep 2000 03:00:00 GMT  
 trouble with INSPECT

Quote:


> > But if WS-COMM-NAME happens to contain one or more HIGH-VALUES
> > your count is wrong! :-(

> > We could add another INSPECT which converts HIGH-VALUE to SPACE
> > before your suggested code. That should assure a correct result.

> > Not bad! Four INSPECTS in a row. Is that a record?

> For inefficiency.

> How about this:

> 01 WS-COMM-NAME2    PIC X(30).
> 01  WS-ALPHA         PIC  X(53) VALUE
>         "*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
> 01  WS-STARS         PIC  X(53) VALUE
>         " ****************************************************".

> MOVE WS-COMM-NAME TO WS-COMM-NAME2
> INSPECT WS-COMM-NAME2 CONVERTING WS-ALPHA TO WS-HIGH-STARS
> INSPECT WS-COMM-NAME2 TALLING KEY-CNT FOR ALL "*"

> Two inspects rather than four.  Unless converting lower case to upper
> was also part of the exercise.

And unless you wanted to have the VALUE of the field remain in tact.
This approach is a bit destructive.


Fri, 29 Sep 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. student having trouble with inspect tallying

2. Inspecting SYSTEMTIME new

3. Nested inspect statements

4. 'Unmatched Inspect Value' Error

5. Inspecting a wordlist's words

6. inspecting leds

7. Inspect dynamic memory in Oberon/F

8. Inspecting the runnable saved state of a pop process

9. "inspect" and constants

10. inspect statemen

11. Inspecting a caller environment?

12. inspect object attributes

 

 
Powered by phpBB® Forum Software