Thomson S
11th May 2013, 12:27
Hi,
I have a requirement where in I will receive a request for a Stock availability check for multiple items as a Single string. The string which i will receive contains a common field 'message Id' and 'track id' present at the first position and second position respectively. followed by multiple item details. and at the end again common fields 'language' and 'user'. The string contains 25 fields.. first two is 'message id and track id' and last two is 'User and language'.

In between those fields i ill have looped fields. like below.

messageid|trackid|Item1|quantity|unit|price|customer|Item2|quantity|unit|price|customer|Item3|quantity|unit|price|customer|User|language.

I have to check the stock available for each item and send back as a response details.

How can i loop the inbetween fields as first two and last two are common for a single string. I have to read the string into seperate fields. and process it individually... how can i loop it. please help me.

Note: the last two fields are optional. it may or maynot come in the string.

vinceco252
11th May 2013, 20:45
If it's a delimited string, you can look at the string.scan function. Do a search for it here.

Vince

Thomson S
13th May 2013, 07:50
Yes it is delimited string. I used string.scan function, but it can split the string into individual fields, but how can i loop using string.scan function.

mark_h
13th May 2013, 15:10
Well what you could do - and I have not done this, but just typing it in as I think it. Is to get the first two fields you could use string.scan("%s|%s|%s",messageid,trackid,somebuff). This gets the first two fields and somebuff would have the balance of the data in the string. Then you could process each of the 3 items(assumption from your record posted):

|Get first two fields
string.scan("%s|%s|%s",messageid,trackid,somebuff)
for i = 1 to 3
scan.ret = string.scan("%s|%s|%s|%s|%s|%s", item,qty, unit, price, cust, somebuff
1)
| Process the item and do what ever you need to do.
somebuff = somebuff1
endfor
| Get last two optional fields
if not isspace(somebuff) then
scan.ret = string.scan("%s|%s|", user, lang)
endif

I used two buffers because I am not sure what would happen if you parsed into the same buffer you are reading. This is what I would start with and I think you can tweak it to do what you need. Of course I assumed you would always have the same number of parts between first 2 and last 2 fields.

Thomson S
14th May 2013, 06:21
Thank you, I got atleast a spark. Yes, the first two and last two will remain same. but inbetween item will go iterate, maximum of 10 times and minimun of 1 time, it varies. 1-10 times i may come in any numbers :)

sameer.don
14th May 2013, 09:07
Thomson,

You can make use of pos() function. It searches for the position of a sub-string ("|" will be substring in your case) in another string.
Have a array of string to read individual items of string. Use 2 buffers as did Mark.
Make use of "while" loop, which will end when no more "|" separator left in the buffer used for searching. I mean, you may use the logic of reading from stack.

Let me know if it helps.

Thomson S
16th May 2013, 13:14
Thats a great idea. I will combine both of your comments and make it work. i hope it will work. Thanks a lot both...