separate string (tab delimited)

julkifli33julkifli33 Member Posts: 1,092
hi all...
i have a txt file, which can be read by ";" delimited
SFile.READ(vString);
PosCode:=STRPOS(vString,';');
vStringTransaction := FORMAT(COPYSTR(vString,1,PosCode-1));
vString:=DELSTR(vString,1,PosCode);

how to differentiate the file... if the delimited using TAB?
thanks

Comments

  • MaximusMaximus Member Posts: 105
    Hi,

    have you tried the following?

    Define a variable called TAB of Type Char

    Assign a value with code like this: TAB := 9;

    Then reuse your code like this:

    SFile.READ(vString);
    PosCode:=STRPOS(vString,FORMAT(TAB));
    vStringTransaction := FORMAT(COPYSTR(vString,1,PosCode-1));
    vString:=DELSTR(vString,1,STRLEN(vStringTransaction) + STRLEN(FORMAT(TAB)));
  • julkifli33julkifli33 Member Posts: 1,092
    i tried that before
    but maybe because the delchr --> posode-1
    there is an error message like this
  • vaprogvaprog Member Posts: 1,141
    If you read from a file with TEXMODE=TRUE, tabs will be converted to spaces with tab stops at position 9, 17, 25... Therefore, you will not find any tab characters anymore. Instead, use InStream.READTEXT and a field separator string according to Maximus's suggestion.

    Furthermore, you should guard COPYSTR and DELSTR against the case where STRPOS did not find a field separator (i.e. PosCode=0).

    At last, I believe FORMAT without additional parameters does not do anything useful in the construction FORMAT(COPYSTR(....
  • julkifli33julkifli33 Member Posts: 1,092
    hi vaprog,
    i tried
        SFile.READ(vString);
        PosCode := STRPOS(vString,FORMAT(Tab));
        vStringTransaction := COPYSTR(vString,1,PosCode);
        MESSAGE('%1 - %2',PosCode,vStringTransaction);
    
    but the poscode still 0
    any clue?
  • MMVMMV Member Posts: 99
    hi julkifli33,

    I created a text file with three "TAB" characters and tried to read it and it is able to recognise the TAB correctly. Pls try the below code.

    //FileStream InStream
    //FileTest File
    //txt Text 250
    //tabchar char

    tabchar := 9;

    FileTest.OPEN('C:\Users\MMV\Documents\MMV\Misc\tabdelimited.txt');
    FileTest.CREATEINSTREAM(FileStream);
    WHILE NOT (FileStream.EOS()) DO
    BEGIN
    FileStream.READTEXT(txt,1);
    IF FORMAT(txt) = FORMAT(tabchar) THEN
    MESSAGE('yes');
    END;
    FileTest.CLOSE();

    Best Regards,
    MMV
  • vaprogvaprog Member Posts: 1,141
    julkifli33 wrote:
    any clue?
    Have you read my post?
    vaprog wrote:
    use InStream.READTEXT

    See MMV's code for reading and looping, then use your code within the loop.
    You can safely read a full line of text, as long as you can guarantee that each and every line fits into your text variable. Else you need to read the line in chunks, but this is not much different from reading from a file, only more flexible with streams.
  • julkifli33julkifli33 Member Posts: 1,092
    MMV wrote:
    hi julkifli33,

    I created a text file with three "TAB" characters and tried to read it and it is able to recognise the TAB correctly. Pls try the below code.

    //FileStream InStream
    //FileTest File
    //txt Text 250
    //tabchar char

    tabchar := 9;

    FileTest.OPEN('C:\Users\MMV\Documents\MMV\Misc\tabdelimited.txt');
    FileTest.CREATEINSTREAM(FileStream);
    WHILE NOT (FileStream.EOS()) DO
    BEGIN
    FileStream.READTEXT(txt,1);
    IF FORMAT(txt) = FORMAT(tabchar) THEN
    MESSAGE('yes');
    END;
    FileTest.CLOSE();

    Best Regards,
    MMV

    Hi MMV,
    i just curious how to make this happen
    Data :
    CUST001 ITEM001 8
    CUST001 ITEM002 2
    CUST001 ITEM003 10

    so it will prompt message word by word
    -->
    first CUST001, and then ITEM001 and then 8.... and then CUST001.... and so on
    how to get the word as TAB Delimited as a separator
  • MMVMMV Member Posts: 99
    //FileStream InStream
    //FileTest File
    //txt Text 250
    //tabchar Char
    //WholeText Text 1024
    //nextPOS Integer
    //tempChar Char
    //LineFeed Char
    //CarriageReturn Char

    tabchar := 9;
    nextPOS := 0;
    LineFeed := 10;
    CarriageReturn := 13;

    FileTest.OPEN('C:\Users\MMV\Documents\MMV\Misc\tabdelimited2.txt');
    FileTest.CREATEINSTREAM(FileStream);
    WHILE NOT (FileStream.EOS()) DO
    BEGIN
    FileStream.READTEXT(txt,1);
    FileTest.SEEK(nextPOS);
    FileTest.READ(tempChar);
    nextPOS += 1;

    IF tempChar IN [tabchar,LineFeed,CarriageReturn] THEN BEGIN
    MESSAGE('Whole Text %1',WholeText);
    WholeText := '';
    IF tempChar IN [LineFeed,CarriageReturn] THEN
    nextPOS += 1;
    END ELSE
    WholeText += txt;
    END;
    FileTest.CLOSE();


    Though the above code is a round about mechanism, it works well based on the data you have mentioned in the post (only TAB delimited).
Sign In or Register to comment.