View previous topic :: View next topic
|
Author |
Message |
trgdhilip
New User
Joined: 24 Dec 2007 Posts: 3 Location: chennai
|
|
|
|
Hi all,
I have a scenario like this.
I have a variable declaration like this.
01 ws-var1 pic x(30) value 'aaaaaAAAAA vvvvvv'.
I have to find out the number of lowercase letters present in this variable.
Is it possible to do it in cobol?
Thanks,
Dhilip |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Code: |
05 IN-VAR PIC X(30)
VALUE 'abcde ABCDE fghij FGHIJKLMNOPQ'.
05 WS-LC-COUNT PIC 9(04) VALUE ZERO.
05 WS-IN-LOC PIC 9(02).
05 WS-VAR-LOC PIC 9(02).
05 WS-LC-VARS PIC X(26)
VALUE 'abcdefghijklmnopqrstuvwxyz' .
PERFORM
VARYING WS-IN-LOC
FROM 1 BY 1
UNTIL WS-IN-LOC > 30
PERFORM
VARYING WS-VAR-LOC
FROM 1 BY 1
UNTIL WS-VAR-LOC > 26
IF IN-VAR (WS-IN-LOC : 1) =
WS-LC-VARS (WS-VAR-LOC : 1)
ADD 1 TO WS-LC-COUNT
END-IF
END-PERFORM
END-PERFORM
. |
|
|
Back to top |
|
|
Cristopher
New User
Joined: 31 Jul 2008 Posts: 53 Location: NY
|
|
|
|
Dhilip,
You can try this aswell -
Code: |
Z= 0
K= 0
PERFORM UNTIL Z = (LENGTH OF D)
COMPUTE Z = Z + 1
IF FUNCTION UPPER-CASE (D(Z:1)) NOT EQUAL D(Z:1)
COMPUTE K = K + 1
END-IF
END-PERFORM |
The value of K will have the number of lower case elements encountered.
Hope this helps.
Cris |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
You can achieve this by using intrinsic function ORD
below is the code
Code: |
*DD
01 MYVAR.
02 UL PIC X(30) VALUE 'aaaaaAAAAA vvvvvv'.
02 UL1 REDEFINES UL PIC X OCCURS 30 TIMES.
01 LOWERCNT PIC 99 VALUE 0.
01 LCNT PIC 99 VALUE 1.
*PD
PERFORM CNTLOWER 30 TIMES.
DISPLAY "LOWER CASE CNT:" LOWERCNT.
STOP RUN.
CNTLOWER.
IF FUNCTION ORD(UL1(LCNT)) >= FUNCTION ORD('a') AND
FUNCTION ORD(UL1(LCNT)) <= FUNCTION ORD('z') THEN
COMPUTE LOWERCNT = LOWERCNT + 1.
COMPUTE LCNT = LCNT + 1.
|
|
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Sambhaji: try your code on a string with a tilde (~) in it. Since the EBCDIC collating sequence is not contiguous for the lower case alphabet, your code can pick up spurious lower case values, such as a tilde. To do what the origianl poster wnated, you need three separate comparisons for the range 'a' to 'i', 'j' to 'r', and 's' to 'z' or your count may be off. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
Quote: |
I have to find out the number of lowercase letters present in this variable.
|
Robert Sample's solution works.
Cris, you managed to count the non-uppercase; not the lowercase.
Sambhaji, if you were to check out the ebcdic chart
you will find that there are values between i and j and r and s that are not lowercase.
here is a link: www.lookuptables.com/ebcdic_scancodes.php |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Thanks, Dick -- I tested that code before posting it. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
Robert,
that is the difference between a profi and a chair-weight,
of which you have proven many times over,
that you are a member of the first group.
learned a lot following your posts. |
|
Back to top |
|
|
Cristopher
New User
Joined: 31 Jul 2008 Posts: 53 Location: NY
|
|
|
|
Dick,will this code suit the requirement
Code: |
Z= 0
K= 0
PERFORM UNTIL Z = (LENGTH OF D)
COMPUTE Z = Z + 1
IF FUNCTION LOWER-CASE (D(Z:1)) EQUAL D(Z:1)
COMPUTE K = K + 1
END-IF
END-PERFORM |
Cris |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Hi Cris,
The only aspect of using FUNCTION LOWER-CASE, UPPER-CASE or what have you, is that (under the covers) a BALR is issued to a run-time routine.
IMHO, Robert's solution is all in-line and you never leave the caller.
Regards,
Bill |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
Quote: |
Sambhaji: try your code on a string with a tilde (~) in it. Since the EBCDIC collating sequence is not contiguous for the lower case alphabet, your code can pick up spurious lower case values, such as a tilde |
Thanks Robert for pointing out this, I have missed it
Amendments are done here
Code: |
CNTLOWER.
IF ( FUNCTION ORD(UL1(LCNT)) >= FUNCTION ORD('a') AND
FUNCTION ORD(UL1(LCNT)) <= FUNCTION ORD('i')) OR
( FUNCTION ORD(UL1(LCNT)) >= FUNCTION ORD('j') AND
FUNCTION ORD(UL1(LCNT)) <= FUNCTION ORD('r')) OR
( FUNCTION ORD(UL1(LCNT)) >= FUNCTION ORD('s') AND
FUNCTION ORD(UL1(LCNT)) <= FUNCTION ORD('z')) THEN
COMPUTE LOWERCNT = LOWERCNT + 1.
COMPUTE LCNT = LCNT + 1. |
|
|
Back to top |
|
|
acevedo
Active User
Joined: 11 May 2005 Posts: 344 Location: Spain
|
|
|
|
what about using the SPECIAL NAMES? for example:
Code: |
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
CLASS lowercase IS 'abcdefghijklmnopqrstuvwxyz'
.....
PROCEDURE DIVISION.
move 'a' to w-char
IF W-CHAR IS lowercase
DISPLAY W-CHAR 'lowercase-YES'
ELSE
DISPLAY W-CHAR 'lowercase-NO'
END-IF
...
|
so no need to loop around WS-LC-VARS. |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
acevedo wrote: |
what about using the SPECIAL NAMES? |
My thought, exactly... |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Or the level 88. . .
Code: |
01 THE-CHARACTER PIC X.
88 LC-LETTER VALUES ARE 'a' THRU 'i',
'j' THRU 'r',
's' THRU 'z'.
|
d |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
How about this one more way...
Code: |
CNTLOWER.
if UL1(LCNT) is alphabetic-lower and
UL1(LCNT) not = space then
COMPUTE LOWERCNT = LOWERCNT + 1.
COMPUTE LCNT = LCNT + 1.
|
|
|
Back to top |
|
|
|