parsing text for values

freak

Member
Using 9.1d... I have a text file that comes from an email message that I would like to obtain values from. It is formatted roughly like this...

Code:
Part Number: 1234
Qty: 10
Bin: 34003
I'd like a way for Progress to parse this text, determine the field, ie "Part Number" and obtain the values of these fields. Everything I have thought of is pretty messy. Any tips would be appreciated.
 

GregTomkins

Active Member
/* Hopefully, we can assume there is only ever one colon */
IMPORT x.
y = ENTRY(1,x,":").
z = ENTRY(2,x,":").

/* Hopefully, we can assume the trailing colon is reliable and a colon never appears in the 2nd field */
IMPORT x.
y = R-INDEX(x,":").
z = SUBSTR(x,1,y - 1).
a = SUBSTR(x,y + 1).

/* Disclaimer: code written in about 3 seconds, not subject to my traditional rock-solid QA process */
 

freak

Member
I would like it better if I could search for a field name such as "Part Number" and then grab the value. The problem is that this is an email message, the whole message with all the header info as well. There are tons of colons in the header and the exact number of them is variable.
 

GregTomkins

Active Member
Off the top of my head, you could string up the whole email into a big variable (watch the 32K size limit) and then search for "Part Number" with INDEX. You could have a little array or TT of all the tokens you are looking for and scan for them one by one. Or, you could do the same thing way more efficiently using a utility like 'egrep', if you are on Unix. There are probably a zillion more ways you could do it if you wander outside the Progress world.

Of course, you would still be hoping that people don't send you an email that says something like 'Hi Bill, what's your favorite Part Number?' and possibly causing the rest of your system to generate an invoice for a part number of '?'. To some extent, that is probably unavoidable if you are parsing free-format email.
 

Cringer

ProgressTalk.com Moderator
Staff member
What OS are you on? I'd imagine that parsing the text would be better done by another app, placing it in a format that's easier for Progress to understand. Not used *nix for a long time, but I remember I used to parse CDR records from a telecomms switch on a unix box, before then importing them to the database using Progress code. Just a thought.
 
Top