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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Name Value Pairs

Status
Not open for further replies.

wanpaku

IS-IT--Management
Jan 22, 2002
2
US
I have data being populated in a text file that I need to parse.
It is stored as a single string of name value pairs delimited by a semi-colon.

ie
order_number=2800540;sub_total=000040330;shipping=0002695;additional_shipping=0000000;order_total=00000043025;customer_number=002381641;sku=49785;item_id=33750;list_price=0043025;

(There are additional NVP's that are not in this example and it is not a consitant order).
What is the easiest way to pull the individual names and values out so it can be put into a table?

tia
 
Using FILETOSTR() and then ALINES() in VFP 7.0 would make this pretty easy. If you have 6.0 or earlier, it's just a bit more code.

Rick
 
This will work:

order_number = ;
"2800540;sub_total=000040330;shipping=0002695;"+;
"additional_shipping=0000000;order_total=00000043025;"+;
"customer_number=002381641;sku=49785;item_id=33750;"+;
"list_price=0043025"

lnArrayRows = ;
alines(laAccountDetail, strtran(order_number, ";" , chr(13) ) )

* This is what the array looks like after running the previous code:

laArrayDetail[1] = "2800540"
laArrayDetail[2] = "sub_total=000040330"
laArrayDetail[3] = "shipping=0002695"
laArrayDetail[4] = "additional_shipping=0000000"
laArrayDetail[5] = "order_total=00000043025"
laArrayDetail[6] = "customer_number=002381641"
laArrayDetail[7] = "sku=49785"
laArrayDetail[8] = "item_id=33750"
laArrayDetail[9] = "list_price=0043025"

* Then it's just a matter of parsing the field=value pairs in the 2nd through last array rows.

i.e.:

for lnRowCount = 2 to alen(laArrayDetail,1)

lcField = left(laArrayDetail[lnRowCount],at("=",laArrayDetail[lnRowCount])-1)

lcValue = substr(laArrayDetail[lnRowCount],at("=",laArrayDetail[lnRowCount])+1)

* Do something with the data - like insert into a cursor/table, etc.

next

Darrell
'We all must do the hard bits so when we get bit we know where to bite' :)
 
Sorry, I changed laAccoutDetail to laArrayDetail.
You probably figured that out.

Darrell 'We all must do the hard bits so when we get bit we know where to bite' :)
 
HI

cString = STRTOFILE(myTextFile)

nLines=ALINES(laArray,cString,.t.,";")
FOR I=1 TO nLines
mVarName = substr(laArray(I),1,at("=",laArray(I))-1)
mVarValue = substr(laArray(I),at("=",laArray(I))+1)
STORE mVarValue TO &mVarName
ENDFOR

DISPLAY MEMORY

But remember the Values are all coming as STRING i.e. Character values. You can add in functionality to check if all are digits and then convert them to numerals.

:) ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top