Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a field backwards

Status
Not open for further replies.

jmoore207

Programmer
Aug 23, 2007
13
US
I am working on a data conversion project and I am trying to find the zip in the field(s). It can be in adr3 or adr2 field and I have to figure out how to parse it out to a zip field. The problem is the data is not consistent. Here are some examples of what the data looks like. The input file is pic x(48) for adr1, adr2, adr3. We are moving to a file where the maximum field length is 28. If zip is not in adr3 than I need to look for it in adr2.

Suite 160 Phoenix AZ 85018
Phoenix AZ 85019
Phoenix AZ 85019
Phoenix AZ 85019-11111
Phoenix AZ 85019 11111

Any help would be appreciated!
 
Thanks, for getting back to me Glenn! When I go to that page I can't see the examples. Not sure why. I will search for unstring with pointers. Thanks!
 
Glenn can you post a little pseudocode or an example.
 
Move your address field to a temporary of the same size.

Inspect the temporary field converting "0123456789" to ALL "*"

Inspect the temporary field tallying for characters before initial "*****"

If you don't find "*****" then zip is in the other field. Rinse and repeat.

If you do find "*****" then the tallying value can be used in reference modification to grab it from the address field.

Tom Morrison
 
It seems I may need to unstring and then go back and inspect it. I need a good example of unstring using pointer and tallying. Since the data is never in the same format. I went to the page Glenn recommended but I cannot view the examples.
 
This may be oversimplified, but if the state code is surrounded by spaces ...

Unstring data-rec delimited by " AZ " or " AR " or " AL "
(etc - all state codes) into field1, field2.

Field2 should contain your zip code.
 
Try searching this forum for the UNSTRING verb.

Glen9999 gave a good example of the UNSTRING in the following topic thread209-1520795.

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Here's how I would do it:

1) Search backwards for the last non-blank character.

2) check the last 4 characters (not 5 per the example) for numeric.

3) if they are, check the preceding character for numeric.
a) if the preceding character is not numeric, it is zip+4
b) if the preceding character is numeric, it is just the zip code.
 
If you don't find "*****" then zip is in the other field. Rinse and repeat.

What if you have a 5 digit street address? For example, this lovely home in Scottsdale, AZ?


I need a good example of unstring using pointer and tallying

I'm not sure using WITH POINTER would be the easiest approach with a problem like you have. Below is a snippet out of a word counting problem we had in this forum a few years ago. You have to initialize the variable, but what TALLYING does is tell you the actual number of fields that were used with the unstring. Later on in the program, the table is processed with a loop using WORDS-IN-LINE as a maximum.

Code:
    04  WORDS-IN-LINE        PIC S9(5) COMP-3.

* PICK APART STRING INTO WORDS
     MOVE 0 TO WORDS-IN-LINE.
     UNSTRING KJV-DATA
       DELIMITED BY ALL SPACES
            INTO Each-Word (1)  Each-Word (2)  Each-Word (3)
                 Each-Word (4)  Each-Word (5)  Each-Word (6)
                 Each-Word (7)  Each-Word (8)  Each-Word (9)
                 Each-Word (10) Each-Word (11) Each-Word (12)
                 Each-Word (13) Each-Word (14) Each-Word (15)
                 Each-Word (16) Each-Word (17) Each-Word (18)
                 Each-Word (19) Each-Word (20)
          TALLYING WORDS-IN-LINE
      END-UNSTRING.

WITH POINTER is simply specified to keep track of a starting point. Instead of doing it all at once, you can unstring segments within a loop until the WITH POINTER value (again numeric) hits the length of the variable you are UNSTRINGing.

Measurement is not management.
 
Wow, thanks for all your great feedback. I think I might have it figured out!
 
Instead of figuring out how to use for it UNSTRING you can try write your own algorithm for finding ZIP.

I don't know about ZIP-Code, but the following program works with data you posted:

zipcode.cbl
Code:
       [COLOR=#804040][b]IDENTIFICATION[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]PROGRAM-ID[/b][/color]. ZIPCODE.
       [COLOR=#804040][b]AUTHOR[/b][/color]. mikrom.
       [COLOR=#804040][b]ENVIRONMENT[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]CONFIGURATION[/b][/color][COLOR=#804040][b] SECTION[/b][/color].
       [COLOR=#804040][b]SOURCE-COMPUTER[/b][/color]. IBM-ISERIES.
       [COLOR=#804040][b]OBJECT-COMPUTER[/b][/color]. IBM-ISERIES.
       [COLOR=#804040][b]SPECIAL-NAMES[/b][/color].
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DECIMAL-POINT[/b][/color] [COLOR=#804040][b]IS[/b][/color] [COLOR=#804040][b]COMMA[/b][/color]
      [COLOR=#6a5acd]     [/color].

       [COLOR=#804040][b]DATA[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]FILE[/b][/color][COLOR=#804040][b] SECTION[/b][/color].
       [COLOR=#804040][b]WORKING-STORAGE[/b][/color][COLOR=#804040][b] SECTION[/b][/color].
       [COLOR=#2e8b57][b]01 [/b][/color]INPUT-ADDRESS          [COLOR=#804040][b]PIC[/b][/color] X([COLOR=#ff00ff]40[/color]).
       [COLOR=#2e8b57][b]01 [/b][/color]WS-FIELDS.
          [COLOR=#2e8b57][b]05 [/b][/color]WS-ADDRESS          [COLOR=#804040][b]PIC[/b][/color] X([COLOR=#ff00ff]40[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]WS-ADDRESS-LENGTH   [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]02[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]WS-ZIPCODE          [COLOR=#804040][b]PIC[/b][/color] X([COLOR=#ff00ff]12[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]ZIP-START           [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]02[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]ZIP-END             [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]02[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]ZIP-LENGTH          [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]02[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]MINIMAL-ZIP-LENGTH  [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]02[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]J                   [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]02[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]WS-DATA             [COLOR=#804040][b]PIC[/b][/color] X([COLOR=#ff00ff]80[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]WS-DATA-LENGTH      [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]02[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]X                   [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]02[/color]).

       [COLOR=#804040][b]PROCEDURE[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]MAIN[/b][/color].
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]'Suite 160 Phoenix AZ 85018'[/color] [COLOR=#804040][b]TO[/b][/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'Address : '[/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#008080]PERFORM[/color] PARSE-ZIPCODE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'ZIP-Code: '[/color] WS-ZIPCODE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]'Phoenix AZ 85019'[/color]           [COLOR=#804040][b]TO[/b][/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'Address : '[/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#008080]PERFORM[/color] PARSE-ZIPCODE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'ZIP-Code: '[/color] WS-ZIPCODE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]'Phoenix AZ  85019'[/color] [COLOR=#804040][b]TO[/b][/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'Address : '[/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#008080]PERFORM[/color] PARSE-ZIPCODE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'ZIP-Code: '[/color] WS-ZIPCODE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]'Phoenix AZ  85019 11111'[/color]    [COLOR=#804040][b]TO[/b][/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'Address : '[/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#008080]PERFORM[/color] PARSE-ZIPCODE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'ZIP-Code: '[/color] WS-ZIPCODE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]'Phoenix AZ  85019-11111'[/color]    [COLOR=#804040][b]TO[/b][/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'Address : '[/color] INPUT-ADDRESS
      [COLOR=#6a5acd]     [/color][COLOR=#008080]PERFORM[/color] PARSE-ZIPCODE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'ZIP-Code: '[/color] WS-ZIPCODE
      [COLOR=#0000ff]*[/color]
      [COLOR=#6a5acd]     [/color][COLOR=#008080]GOBACK[/color]
      [COLOR=#6a5acd]     [/color].

       [COLOR=#804040][b]PARSE-ZIPCODE[/b][/color].
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]INITIALIZE[/b][/color] WS-FIELDS
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]MOVE[/b][/color] INPUT-ADDRESS [COLOR=#804040][b]TO[/b][/color] WS-ADDRESS
      [COLOR=#0000ff]*    Length of Address[/color]
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]MOVE[/b][/color] WS-ADDRESS [COLOR=#804040][b]TO[/b][/color] WS-DATA
      [COLOR=#6a5acd]     [/color][COLOR=#008080]PERFORM[/color] COMPUTE-LENGTH-OF-DATA
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]MOVE[/b][/color] WS-DATA-LENGTH [COLOR=#804040][b]TO[/b][/color] WS-ADDRESS-LENGTH
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'Length of Adddress: '[/color] WS-ADDRESS-LENGTH
      [COLOR=#0000ff]*    Find start and end position of ZIP[/color]
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]IF[/b][/color] WS-ADDRESS(WS-ADDRESS-LENGTH:[COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]IS[/b][/color] [COLOR=#804040][b]NUMERIC[/b][/color]
      [COLOR=#0000ff]*       if possible ZIP then search for its start position[/color]
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]MOVE[/b][/color] WS-ADDRESS-LENGTH [COLOR=#804040][b]TO[/b][/color] ZIP-END
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]5[/color] [COLOR=#804040][b]TO[/b][/color] MINIMAL-ZIP-LENGTH
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]COMPUTE[/b][/color] J = ZIP-END - [COLOR=#ff00ff]1[/color]
      [COLOR=#0000ff]*       parse first portion of ZIP[/color]
      [COLOR=#6a5acd]        [/color][COLOR=#008080]PERFORM[/color] [COLOR=#804040][b]UNTIL[/b][/color] WS-ADDRESS(J:[COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]IS[/b][/color] [COLOR=#804040][b]NOT[/b][/color] [COLOR=#804040][b]NUMERIC[/b][/color] [COLOR=#804040][b]OR[/b][/color] J = [COLOR=#ff00ff]0[/color]
      [COLOR=#6a5acd]          [/color][COLOR=#804040][b]COMPUTE[/b][/color] J = J - [COLOR=#ff00ff]1[/color]
      [COLOR=#6a5acd]        [/color][COLOR=#008080]END-PERFORM[/color]
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]IF[/b][/color] (WS-ADDRESS(J:[COLOR=#ff00ff]1[/color]) = [COLOR=#ff00ff]'-'[/color] [COLOR=#804040][b]OR[/b][/color] [COLOR=#ff00ff]' '[/color]) [COLOR=#804040][b]AND[/b][/color]
      [COLOR=#6a5acd]            [/color]WS-ADDRESS(J - [COLOR=#ff00ff]1[/color]:[COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]IS[/b][/color] [COLOR=#804040][b]NUMERIC[/b][/color]
      [COLOR=#0000ff]*           if second portion of zip exists then continue[/color]
      [COLOR=#6a5acd]            [/color][COLOR=#804040][b]COMPUTE[/b][/color] J = J - [COLOR=#ff00ff]1[/color]
      [COLOR=#6a5acd]            [/color][COLOR=#008080]PERFORM[/color] [COLOR=#804040][b]UNTIL[/b][/color] WS-ADDRESS(J:[COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]IS[/b][/color] [COLOR=#804040][b]NOT[/b][/color] [COLOR=#804040][b]NUMERIC[/b][/color] [COLOR=#804040][b]OR[/b][/color] J = [COLOR=#ff00ff]0[/color]
      [COLOR=#6a5acd]              [/color][COLOR=#804040][b]COMPUTE[/b][/color] J = J - [COLOR=#ff00ff]1[/color]
      [COLOR=#6a5acd]            [/color][COLOR=#008080]END-PERFORM[/color]
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]END-IF[/b][/color]
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]COMPUTE[/b][/color] ZIP-START = J + [COLOR=#ff00ff]1[/color]
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]COMPUTE[/b][/color] ZIP-LENGTH = ZIP-END - ZIP-START + [COLOR=#ff00ff]1[/color]
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]IF[/b][/color] ZIP-START [COLOR=#804040][b]NOT[/b][/color] = ZIP-END [COLOR=#804040][b]AND[/b][/color]
      [COLOR=#6a5acd]           [/color]ZIP-LENGTH >= MINIMAL-ZIP-LENGTH
      [COLOR=#6a5acd]           [/color][COLOR=#804040][b]MOVE[/b][/color] WS-ADDRESS(ZIP-START:ZIP-LENGTH) [COLOR=#804040][b]TO[/b][/color] WS-ZIPCODE
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]END-IF[/b][/color]
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]ELSE[/b][/color]
      [COLOR=#6a5acd]        [/color][COLOR=#804040][b]DISPLAY[/b][/color] [COLOR=#ff00ff]'ZIP was not found !'[/color]
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]END-IF[/b][/color]
      [COLOR=#6a5acd]     [/color].

       [COLOR=#804040][b]COMPUTE-LENGTH-OF-DATA[/b][/color].
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]COMPUTE[/b][/color] X = [COLOR=#ff00ff]0[/color].
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]INSPECT[/b][/color] FUNCTION REVERSE(WS-DATA)
      [COLOR=#6a5acd]             [/color][COLOR=#804040][b]TALLYING[/b][/color] X
      [COLOR=#6a5acd]             [/color][COLOR=#804040][b]FOR[/b][/color] [COLOR=#804040][b]LEADING[/b][/color] [COLOR=#ff00ff]SPACES[/color].
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]COMPUTE[/b][/color] WS-DATA-LENGTH = [COLOR=#804040][b]LENGTH[/b][/color] [COLOR=#804040][b]OF[/b][/color] WS-DATA - X
      [COLOR=#6a5acd]     [/color].
Output:
Code:
  > call zipcode                           
    Address : Suite 160 Phoenix AZ 85018   
    Length of Adddress: 26           
    ZIP-Code: 85018                  
    Address : Phoenix AZ 85019       
    Length of Adddress: 16           
    ZIP-Code: 85019                  
    Address : Phoenix AZ  85019      
    Length of Adddress: 17           
    ZIP-Code: 85019                  
    Address : Phoenix AZ  85019 11111
    Length of Adddress: 23           
    ZIP-Code: 85019 11111            
    Address : Phoenix AZ  85019-11111
    Length of Adddress: 23           
    ZIP-Code: 85019-11111
 
If your Compiler supports the INSPECT verb and the REVERSE Intrinsic Function you can try something like this (untested):


01 L PIC 9(02).
01 WRK-ZIP-FLD.
05 WRK-ZIP-1 PIC 9(05)
05 WRK-DASH PIC X(01)
05 WRK-ZIP-2 PIC 9(05)

INSPECT FUNCTION REVERSE(YOUR-ADDR-FLD) TALLYING L FOR LEADING SPACES
COMPUTE PTR-TO-ZIP = LENGTH OF YOUR-ADDR-FLD – (L + 10)
MOVE YOUR-ADDR-FLD(PTR-TO-ZIP:11) TO WRK-ZIP-FLD
IF WRK-ZIP-1 IS NUMERIC
AND
WRK-SEP = ('-' OR ' ')
AND
WRK-ZIP-2 IS NUMERIC
You have an 11 byte ZIP in WRK-ZIP-FLD
ELSE
IF WRK-ZIP-1 IS NUMERIC
You have a 5 byte ZIP in WRK-ZIP-FLD
ELSE
Try this mess again on the 2nd ADDR-FLD
END-IF
END-IF

In English, the INSPECT stmt finds the last non-blank char in the addr field.
The COMPUTE finds the beginning of the 5 or 11 pos ZIP.
The MOVE assumes a 5 OR 11 ZIP and puts it and prepares it for analysis.
The 1st IF looks for an 11 ZIP, the 2nd for a 5; if neither do it again in ADDR-3.


PS - I thought it was ZIP+4, but I went w/5 JIC.

Regards, Jack.

"A problem well stated is a problem half solved" -- Charles F. Kettering
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top