Eliminating hyphen in Strings



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

Eliminating hyphen in Strings

Postby Vikrant Survase » Tue Jun 08, 2010 12:28 am

Suppose I have a word as shown v-i-k-r-a-n-t and I want it as only vikrant eliminating hyphen '-' or any other special characters. How can I do this?
Vikrant Survase
 
Posts: 19
Joined: Fri Jun 04, 2010 9:08 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Eliminating hyphen in Strings

 

Re: Strings

Postby dick scherrer » Tue Jun 08, 2010 2:01 am

Hello,

Keep in mind that mainframe cobol does not "do" strings. . . What you have is an alphanumeric field that you want to reformat.

One easy way to so what you want is to code a loop that processes this field one byte at a time (using reference modification) and move the non ('-' or whatever you consider special characters) bytes to another alphanumeric field (again using reference modification).
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times

Re: Strings

Postby dick scherrer » Tue Jun 08, 2010 2:03 am

Or you could defined all of the bytes you want to "keep" and use this to control the process.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times

Re: Strings

Postby ajmal1811 » Tue Jun 08, 2010 3:22 pm

Hi,

I'm new to mainframes(Trainee) and to this forum. Following is what i've tried. I couldnt compile and run it because i dont have access to mainframes right now. Please correct me if i'm wrong. Thank you.

 WORKING-STORAGE SECTION.
 01 WS-WORKING-VAR PIC X(13) VALUE 'V-I-K-R-A-N-T'.
 01 WS-FINAL-VAR       PIC X(7) VALUE SPACES.
 01 WS-COUNT            PIC 9.
 01 X                           PIC 9 VALUE 1.
 01 Y                           PIC 9 VALUE 1.
 PROCEDURE DIVISION.
 MAIN-PARA.
     MOVE 0 TO WS-COUNT.
     INSPECT WS-WORKING-VAR TALLYING WS-COUNT FOR ALL '-'.
     PERFORM PROCESS-PARA THRU PROCESS-PARA-EXIT UNTIL WS-COUNT = 0.
     DISPLAY WS-FINAL-VAR.
     STOP RUN.

 PROCESS-PARA.
     MOVE WS-WORKING-VAR(X:1) TO WS-FINAL-VAR(Y:1)
     ADD 2 TO X.
     ADD 1 TO Y.
     SUBTRACT 1 FROM WS-COUNT.
 PROCESS-PARA-EXIT.
     EXIT.
ajmal1811
 
Posts: 2
Joined: Fri Jun 04, 2010 11:40 am
Has thanked: 0 time
Been thanked: 0 time

Re: Strings

Postby Robert Sample » Tue Jun 08, 2010 4:48 pm

Your program output is
 VIKRAV


The first problem with your code is that it is very specific to the data -- an extra dash in the data would completely change your results. And the problem statement was to remove hyphens "or other special characters" which your code does not do.

The second problem you have is that your variable is 13 bytes yet your subscripts are only one-byte variables. So X goes from 1 to 3 to 5 to 9 to ... 1 again since a one-byte variable won't ever be more than 9.

The problem statement provided is woefully inadequate. How long can the input variable be? Can it have numbers? if so, are numbers considered valid or invalid characters? What are the invalid characters?
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 222 times

Re: Strings

Postby Vikrant Survase » Tue Jun 08, 2010 8:37 pm

Robert Sample wrote:Your program output is
 VIKRAV


The first problem with your code is that it is very specific to the data -- an extra dash in the data would completely change your results. And the problem statement was to remove hyphens "or other special characters" which your code does not do.

The second problem you have is that your variable is 13 bytes yet your subscripts are only one-byte variables. So X goes from 1 to 3 to 5 to 9 to ... 1 again since a one-byte variable won't ever be more than 9.

The problem statement provided is woefully inadequate. How long can the input variable be? Can it have numbers? if so, are numbers considered valid or invalid characters? What are the invalid characters?



hey
invalid characters are special character's also the numbers, i want only alphabets & for length of input variable is there any clause which decides the allocation of bytes according to input variable length?
else the above code is almost correct.!
Vikrant Survase
 
Posts: 19
Joined: Fri Jun 04, 2010 9:08 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Strings

Postby Robert Sample » Tue Jun 08, 2010 9:00 pm

Code that is "almost correct" is more properly called wrong code. Either the code does precisely what the problem statement requires, or the code is not right. Close counts in horseshoes and hand grenades, not programming.

WORKING-STORAGE SECTION.
01 WS-WORKING-VAR PIC X(80) VALUE 'V-I-K-R-A-N-T'.
01 WS-FINAL-VAR       PIC X(80) VALUE SPACES.
01 X                           PIC S9(04) COMP VALUE 1.
01 Y                           PIC S9(04) COMP VALUE 1.
PROCEDURE DIVISION.
MAIN-PARA.
    PERFORM VARYING X FROM 1 BY 1
        UNTIL X > LENGTH OF WS-WORKING-VAR
        IF  WS-WORKING-VAR (X : 1) ALPHABETIC
            MOVE WS-WORKING-VAR (X : 1) TO  WS-FINAL-VAR (Y : 1)
            ADD 1 TO Y
        END-IF
    END-IF
is a more robust way of accomplishing your stated goal -- although this is not tested code and may require some adjustments when compiled. The 80 is arbitrary and could be higher if needed --if higher than 9999 then X and Y need to be defined with larger PIC clauses.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 222 times

Re: Strings

Postby Vikrant Survase » Tue Jun 08, 2010 9:24 pm

Robert Sample, thanks

but,
The 80 is arbitrary in ur code ok it will do
but if i want to decide the length according to my input string?
Vikrant Survase
 
Posts: 19
Joined: Fri Jun 04, 2010 9:08 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Strings

Postby enrico-sorichetti » Tue Jun 08, 2010 9:35 pm

why not spend a bit of time trying to understand the posted code ?
after that modifying it for a different length will be just a few keyboards clicks away
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: 2644
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 130 times

Re: Strings

Postby Robert Sample » Tue Jun 08, 2010 10:39 pm

but,
The 80 is arbitrary in ur code ok it will do
but if i want to decide the length according to my input string?
COBOL is a compiled language. Before the compile, every variable must be defined to COBOL -- which includes the maximum length allowed. Unlike C and other languages, COBOL does not support dynamic manipulation of variables lengths at run time (although there is a loophole in the way COBOL handles LINKAGE SECTION variables). So you cannot decide the length of the variable at run time -- you must decide how long it can be when you compile the program.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 222 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post