character/string manipulation

Posted by Admin on 13-Jun-2007 08:55

Hello,

I have a text file with variable line lengths and a space as a delimiter. I'm trying to extract 2 characters (qty sold field) from each line. The problem is that one of the fields, product description, has spaces in it, so i can't rely on the space as a delimiter when moving forward from the beginning of the line. But the qty sold field is always the 5th field from the end of the line. So is there a way to read backwards from the end of the line to find the offset?

Any help would be greatly appreciated.

Thanks,

ML

here's an example of my input file:

Style: 30340; Here's A Product Description 1 32.00 19.17 12.83 40.09%

Style: 30397; Another Product Descript 2 64.00 38.34 25.66 40.09%

...and here is my code so far, with psudocode indicated where i need to figure out the value of startindex (indicated by *****):

DEFINE VARIABLE startindex as INT.

DEFINE VARIABLE vc-qtysold AS CHARACTER FORMAT "XX" NO-UNDO.

INPUT FROM C:\temp\inputfile.txt.

OUTPUT TO C:\temp\outputfile.TXT

REPEAT:

IMPORT UNFORMATTED vc-import.

ASSIGN

          • startindex = , , +1

vc-qtysold = SUBSTRING(vc-import,startindex,2).

PUT UNFORMATTED vc-qtysold SKIP.

END

OUTPUT CLOSE.

INPUT CLOSE.

IF SESSION:SET-WAIT-STATE("") THEN.

All Replies

Posted by Admin on 13-Jun-2007 15:40

Take a look at the R-INDEX function (searches for a string in right-to-left order, opposite of INDEX) to find the 5th space from the end.

Or, try a solution using the ENTRY and NUM-ENTRIES functions, both of which have an optional parameter for specifying a list delimiter other than the default comma.

Posted by relgert on 18-Jun-2007 07:30

try this:

DEF VAR vc-import AS CHAR FORMAT "x(100)".

vc-import = "Style: 30397; Another Product Descript 22 64.00 38.34 25.66 40.09%".

DEF VAR nQty AS INT.

DEF VAR nQtyStart AS INT.

DEF VAR nQtyEnd AS INT.

DEF VAR nQtyLength AS INT.

DEF VAR nLength AS INT.

DEF VAR nSpaceCount AS INT.

DEF VAR cTestChar AS CHAR FORMAT "x(1)".

nLength = LENGTH(vc-import).

nSpaceCount = 0.

DO WHILE nLength > 0:

nLength = nLength - 1.

cTestChar = SUBSTRING(vc-import, nLength, 1).

IF cTestChar = " " THEN DO:

nSpaceCount = nSpaceCount + 1.

IF nSpaceCount = 4 THEN DO:

nQtyEnd = nLength + 1.

END.

IF nSpaceCount = 5 THEN DO:

nQtyStart = nLength + 1.

LEAVE.

END.

END.

END.

nQtyLength = nQtyEnd - nQtyStart.

nQty = INT(SUBSTRING(vc-import, nQtyStart, nQtyLength)).

DISPLAY nQty.

This thread is closed