Page 1 of 2

Eliminating hyphen in Strings

PostPosted: Tue Jun 08, 2010 12:28 am
by Vikrant Survase
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?

Re: Strings

PostPosted: Tue Jun 08, 2010 2:01 am
by dick scherrer
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).

Re: Strings

PostPosted: Tue Jun 08, 2010 2:03 am
by dick scherrer
Or you could defined all of the bytes you want to "keep" and use this to control the process.

Re: Strings

PostPosted: Tue Jun 08, 2010 3:22 pm
by ajmal1811
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.

Re: Strings

PostPosted: Tue Jun 08, 2010 4:48 pm
by Robert Sample
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?

Re: Strings

PostPosted: Tue Jun 08, 2010 8:37 pm
by Vikrant Survase
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.!

Re: Strings

PostPosted: Tue Jun 08, 2010 9:00 pm
by Robert Sample
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.

Re: Strings

PostPosted: Tue Jun 08, 2010 9:24 pm
by Vikrant Survase
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?

Re: Strings

PostPosted: Tue Jun 08, 2010 9:35 pm
by enrico-sorichetti
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

Re: Strings

PostPosted: Tue Jun 08, 2010 10:39 pm
by Robert Sample
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.