简体   繁体   中英

Dynamically parsing the text file

I have an ask to import a text file.

Some of the contents in the text file has static occurrences and some has dynamic.

Example:

H0X004010850TPQ0030030030030032021/03/1710:34:450

H100DEVTEST01

H2PQ003003003003003

H3CP001001001001001PP002002002002002

D1DEVTEST01

D2T-010

S1100000

Out of this H0,H1,H2,H3 and S1 comes only once in a file But D1 and D2 can come multiple times.

The below works well if I have static number of contents. But this will fail if my D1 and D2 occurs multiple times.

Could any one help me with some approach to handle this?

DEFINE TEMP-TABLE TT-File 

FIELDS H0 AS CHAR

FIELDS H1 AS CHAR

FIELDS H2 AS CHAR

FIELDS H3 AS CHAR

FIELDS H4 AS CHAR

FIELDS D1 AS CHAR

FIELDS D2 AS CHAR

FIELDS S1 AS CHAR.

DEFINE VARIABLE W-IMPORT-FILE      AS CHARACTER   NO-UNDO.
DEFINE VARIABLE W-COUNT            AS INTEGER     NO-UNDO.


ASSIGN W-IMPORT-FILE = "C:\Temp\Manny.txt".
INPUT FROM VALUE(W-IMPORT-FILE).
    ASSIGN W-COUNT = 0.
    REPEAT TRANSACTION:
        
        CREATE TT-File.            
        IMPORT UNFORMATTED TT-File.H0.
        IMPORT UNFORMATTED TT-File.H1.
        IMPORT UNFORMATTED TT-File.H2.
        IMPORT UNFORMATTED TT-File.H3.
        IMPORT UNFORMATTED TT-File.D1.
        IMPORT UNFORMATTED TT-File.D2.
        IMPORT UNFORMATTED TT-File.S1.                
            
    END.
INPUT CLOSE.

FOR EACH TT-File:
    IF SUBSTRING(TT-File.H1,1,2) = "H0":U THEN
        MESSAGE "Header 0 is available to parse":U. 
END.

Since there's no information on what the headers etc actually do mean it's hard to give a really good answer. If one row/header changes the meaning of another row a temp-table might be a good idea. If every row however is it's own little universe I wouldn't bother with the temp-table but just handle each row by itself.

Something like this (you need to fill in all the blocks of the CASE-statement).

DEFINE VARIABLE W-IMPORT-FILE      AS CHARACTER   NO-UNDO.
DEFINE VARIABLE W-COUNT            AS INTEGER     NO-UNDO.

DEFINE VARIABLE cImport            AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cHeader            AS CHARACTER   NO-UNDO.


ASSIGN W-IMPORT-FILE = "C:\Temp\Manny.txt".
INPUT FROM VALUE(W-IMPORT-FILE).
    ASSIGN W-COUNT = 0.
    REPEAT TRANSACTION:
        IMPORT UNFORMATTED cImport.

        cHeader = SUBSTRING(cImport,1,2).

        CASE cHeader:
            WHEN "H0" THEN DO:

            END.
            WHEN "H1" THEN DO:

            END.
            WHEN "H2" THEN DO:

            END.
            WHEN "H3" THEN DO:

            END.
            WHEN "H4" THEN DO:

            END.
            WHEN "D1" THEN DO:

            END.
            WHEN "D2" THEN DO:

            END.    
            WHEN "S1" THEN DO:

            END.
        END.

        W-COUNT = W-COUNT + 1.             
            
    END.
INPUT CLOSE.

>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM