IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Convert non numeric fields to Numeric fields in COBOL


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Abi

New User


Joined: 27 Jul 2005
Posts: 35
Location: Chennai

PostPosted: Wed Nov 22, 2006 7:14 pm
Reply with quote

One of the filed has values like
111-222-333
111/222/333
111 222 333
111-222/333


I need convert all these values to 11122233.I need to remove all these dashes,slashes and spaces .. and need to convert into a numeric char.

How can i do it ? can i do it by INSPECT verb.Can anybody let me know the syntax

Thanks
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Wed Nov 22, 2006 7:30 pm
Reply with quote

Hi Abi

You can use rerference modification to achieve this.

Code:
WORKING-STORAGE SECTION.
01 ALPHANUM PIC X(11).

01 NUM.
     02 N1 PIC 999.
     02 N2 PIC 999.
     02 N3 PIC 999.

PROCEDURE DIVISION.
     MOVE ALPHANUM(1:3) TO N1.
     MOVE ALPHANUM(5:3) TO N2.
     MOVE ALPHANUM(9:3) TO N3.
     DISPLAY NUM.
     STOP RUN.
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Wed Nov 22, 2006 7:47 pm
Reply with quote

Or you can use the STRING verb also.Here is the syntax.
Code:
WORKING-STORAGE SECTION.                           
 01 ALPHANUM PIC  X(11) VALUE '123 456/789'.       
 01 NUM.                                           
    02 N1       PIC 999.                           
    02 N2       PIC 999.                           
    02 N3       PIC 999.                           
PROCEDURE DIVISION.                                 
MAIN.                                               
    UNSTRING ALPHANUM DELIMITED BY '/' OR '-' OR ' '
          INTO N1, N2, N3                           
    DISPLAY NUM                                     
    STOP RUN.                                       


Thanks
Arun
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Thu Nov 23, 2006 6:18 am
Reply with quote

Hi arcvns,

You need one missing piece to complete your 2 ans:
Code:

01  NUM-NUM REDEFINES NUM PIC S9(9).

As previously written NUM cannot be used in an arith stmt.
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Thu Nov 23, 2006 9:39 am
Reply with quote

Thanks for correcting me.I was not sure if any arithmetic stmts are using this NUM.
Back to top
View user's profile Send private message
Abi

New User


Joined: 27 Jul 2005
Posts: 35
Location: Chennai

PostPosted: Thu Nov 23, 2006 2:14 pm
Reply with quote

Thanks for the replies... I also have datas like

/188-160 4/
026 375 3/
028-625 7


how to convert these into non-numeric
Back to top
View user's profile Send private message
Abi

New User


Joined: 27 Jul 2005
Posts: 35
Location: Chennai

PostPosted: Thu Nov 23, 2006 2:17 pm
Reply with quote

sorry how to convert these into numeric
Back to top
View user's profile Send private message
surya_pathaus

Active User


Joined: 28 Aug 2006
Posts: 110

PostPosted: Thu Nov 23, 2006 2:43 pm
Reply with quote

Hi,

This can work for you for all types of strings which you mentioned...


Code:

MOVE LENGTH OF SOURCE-STRING TO K
MOVE LENGTH OF TARGET-STRING TO J
PERFORM VARYING I FROM K DOWN BY 1 UNTIL K < I
    IF IS NUMERIC(I : 1)
        MOVE SOURCE-STRING(I : 1) TO TARGET-STRING(J : 1)
         SUBTRACT 1 FROM J
    END-IF
END-PERFORM

Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Thu Nov 23, 2006 4:24 pm
Reply with quote

Hi surya

I tried to execute your code and ended up with some compile-time errors.
I think it should be like this
Code:
PERFORM VARYING I FROM K BY -1 UNTIL I < 1   
    IF ALPHANUM(I : 1) IS NUMERIC           
        MOVE ALPHANUM(I : 1) TO RESULT(J : 1)
         SUBTRACT 1 FROM J                   
    END-IF                                   
END-PERFORM                                 


Correct me if I am wrong

Thanks
Arun
Back to top
View user's profile Send private message
surya_pathaus

Active User


Joined: 28 Aug 2006
Posts: 110

PostPosted: Thu Nov 23, 2006 4:39 pm
Reply with quote

Hi Arun,

Ya. That code is not tested. I gave it as algorithm. Just to give the idea about the logic.

Thanks. Tested the code and corrected.

Is it giving proper results as required?
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Thu Nov 23, 2006 4:48 pm
Reply with quote

Ya...its working fine.

Thanks
Arun
Back to top
View user's profile Send private message
Abi

New User


Joined: 27 Jul 2005
Posts: 35
Location: Chennai

PostPosted: Fri Nov 24, 2006 4:50 pm
Reply with quote

Hi arcvns

What should the value of I be .... shld move the the value of K to I ?
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Fri Nov 24, 2006 5:47 pm
Reply with quote

Hi Abi

You dont need to move any particular value to I.You need to move only the lengths of source and target strings to K and J respectively.

Thanks
Arun
Back to top
View user's profile Send private message
Abi

New User


Joined: 27 Jul 2005
Posts: 35
Location: Chennai

PostPosted: Mon Nov 27, 2006 10:29 am
Reply with quote

Hi
I treid to execute the above code .Non-numeric fields were eliminated.But got one problem.

when i converted the value 188-160 4/

in the traget string i got the value as " 1881604" .I ogt additional 3 spaces in the front.How to remove that space.
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Mon Nov 27, 2006 11:33 am
Reply with quote

Hi Abi

In the above code the result will get stored in a numeric variable(right justified with leading zeros) as the target variable I have defined is

01 RESULT PIC S9(18) VALUE 0.

When I tried with your input I am getting 000000000001881604.Is nt that matching with your expected result?

Please correct me if wrong

Thanks
Arun
Back to top
View user's profile Send private message
Abi

New User


Joined: 27 Jul 2005
Posts: 35
Location: Chennai

PostPosted: Wed Nov 29, 2006 6:53 pm
Reply with quote

Hi,
I tried this below code & it i got the expected values.

MOVE LENGTH OF SOURCE-STRING TO K
MOVE 1 TO J

PERFORM VARYING I FROM 1 BY 1 UNTIL I > K
IF ALPHANUM(I : 1) IS NUMERIC
MOVE ALPHANUM(I : 1) TO RESULT(J : 1)
ADD 1 TO J
END-IF
END-PERFORM

Thanks for your suggestions.
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Wed Nov 29, 2006 7:04 pm
Reply with quote

Hi Abi

Please let me know hw did u define your destination variable.is it PIC X or PIC 9?

Thnks
Arun
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Issues Converting From ZD to Signed N... DFSORT/ICETOOL 4
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
Search our Forums:

Back to Top