looping through CSV list
Posted in 2011
A poster asked how, in Informix 4GL, to walk through a comma-separated string like "300000,310010,420011,650934" and test whether any element is below 290000; his attempt used fixed-width substring indexing. Replies offered alternatives rather than a single fix: Marco Greco plugged his SQSL scripting language (a 'for ... in get topix' loop), mpruet posted a C parsing function, and Ian Gumby outlined the generic 4GL approach — scan character by character, build a temp buffer until each comma, convert to an integer and compare (or set a boolean flag). No confirmed working 4GL code or final resolution is recorded.
Auto-generated by DrWatson from the posts below — may be imperfect; read the full thread.
Topics: General Discussion
If i have a string CSV and want to loop through the elements how would i do so? e.g. let topix = "300000,310010,420011,650934" I need to parse this string and determine if it contains a number less than 290000. I,m doing something like: while (top_loop < length(topic)) if (topix[top_loop, top_loop + 6] < 290000) then return 1 end if end while return 0
On 26/01/11 13:56, monkeys paw wrote: > If i have a string CSV and want to loop through the > elements how would i do so? > e.g. > > let topix = "300000,310010,420011,650934" > > I need to parse this string and determine if it contains > a number less than 290000. > > I,m doing something like: > > while (top_loop < length(topic)) > if (topix[top_loop, top_loop + 6] < 290000) then > return 1 > end if > end while > return 0 <BRAG> that can be easily achieved through my scripting language SQSL (see sig), and you don't even need to go through that clumsy type conversion! </BRAG> -- Ciao, Marco ______________________________________________________________________________ Marco Greco /UK /IBM Standard disclaimers apply! Structured Query Scripting Language http://www.4glworks.com/sqsl.htm 4glworks http://www.4glworks.com Informix on Linux http://www.4glworks.com/ifmxlinux.htm
On 1/26/2011 9:50 AM, Marco Greco wrote: > > > On 26/01/11 13:56, monkeys paw wrote: >> If i have a string CSV and want to loop through the >> elements how would i do so? >> e.g. >> >> let topix = "300000,310010,420011,650934" >> >> I need to parse this string and determine if it contains >> a number less than 290000. >> >> I,m doing something like: >> >> while (top_loop< length(topic)) >> if (topix[top_loop, top_loop + 6]< 290000) then >> return 1 >> end if >> end while >> return 0 > > <BRAG> > that can be easily achieved through my scripting language SQSL (see sig), and > you don't even need to go through that clumsy type conversion! > </BRAG> I'm using Informix. I need the language hoop to parse a CSV and compare the individual components to a particular value.
On Jan 26, 7:56 am, monkeys paw <mon...@joemoney.net> wrote: > If i have a string CSV and want to loop through the > elements how would i do so? > e.g. > > let topix = "300000,310010,420011,650934" > > I need to parse this string and determine if it contains > a number less than 290000. > > I,m doing something like: > > while (top_loop < length(topic)) > if (topix[top_loop, top_loop + 6] < 290000) then > return 1 > end if > end while > return 0 I'm guessing that you are using 'c' so here's how I would do it. int parce(char **tpntParm) { int tnum = 0; int sign = 1; char *tpnt = *tpntParm; if (*tpnt == '-') { sign = -1; tpnt++; } while (*tpnt && (*tpnt != ',')) { tnum *= 10; tnum += (*tpnt - '0'); } *tpntParm = tpnt; if ((tnum * sign) < 290000) return 1; else return 0; }
On 26/01/11 16:08, monkeys paw wrote: > On 1/26/2011 9:50 AM, Marco Greco wrote: >> >> >> On 26/01/11 13:56, monkeys paw wrote: >>> If i have a string CSV and want to loop through the >>> elements how would i do so? >>> e.g. >>> >>> let topix = "300000,310010,420011,650934" >>> >>> I need to parse this string and determine if it contains >>> a number less than 290000. >>> >>> I,m doing something like: >>> >>> while (top_loop< length(topic)) >>> if (topix[top_loop, top_loop + 6]< 290000) then >>> return 1 >>> end if >>> end while >>> return 0 >> >> <BRAG> >> that can be easily achieved through my scripting language SQSL (see sig), and >> you don't even need to go through that clumsy type conversion! >> </BRAG> > > I'm using Informix. I need the language hoop to parse a CSV and compare > the individual components to a particular value. > As others have pointed out, the question is - do you want to do it from inside the engine, or do you want to do it from a client? If you want to do it from a client - ie read the file, parse it and then do something with it, like insert values into a database, then you have many options. SQSL could easily do that for you: let topix = "300000,310010,420011,650934" for top_loop in <+ get topix +>; if (top_loop< 290000) then; # do something end if; end for; # do something else -- Ciao, Marco ______________________________________________________________________________ Marco Greco /UK /IBM Standard disclaimers apply! Structured Query Scripting Language http://www.4glworks.com/sqsl.htm 4glworks http://www.4glworks.com Informix on Linux http://www.4glworks.com/ifmxlinux.htm
On 1/26/2011 11:12 AM, mpruet wrote: > On Jan 26, 7:56 am, monkeys paw<mon...@joemoney.net> wrote: >> If i have a string CSV and want to loop through the >> elements how would i do so? >> e.g. >> >> let topix = "300000,310010,420011,650934" >> >> I need to parse this string and determine if it contains >> a number less than 290000. >> >> I,m doing something like: >> >> while (top_loop< length(topic)) >> if (topix[top_loop, top_loop + 6]< 290000) then >> return 1 >> end if >> end while >> return 0 > > I'm guessing that you are using 'c' so here's how I would do it. > > int > parce(char **tpntParm) > { > int tnum = 0; > int sign = 1; > char *tpnt = *tpntParm; > > if (*tpnt == '-') > { > sign = -1; > tpnt++; > } > > while (*tpnt&& (*tpnt != ',')) > { > tnum *= 10; > tnum += (*tpnt - '0'); > } > *tpntParm = tpnt; > if ((tnum * sign)< 290000) > return 1; > else > return 0; > } No, i was referring to 4gl. I don't think i have to interface with C in order to do this. Would 4gl handle this on it's own?
> Date: Wed, 26 Jan 2011 12:52:05 -0500 > From: monkey@joemoney.net > Subject: Re: looping through CSV list > To: informix-list@iiug.org > > No, i was referring to 4gl. I don't think i have to interface with > C in order to do this. Would 4gl handle this on it's own? C would be cleaner and easier. (Ok both are easy...) With respect to 4GL... I haven't touched it since '94-95 time frame so I'm sure that someone will correct me.... You have a string statement "xxxx,xxxx,xxxx,xxxx,xxx,xxx" where each xxx is some number. You'd have to create an array/list to store the individual numbers, two integers i and j. i is used to keep track of your position in the string, j is to keep track of your position in the list. In a loop, you walk through the string character by character looking for the comma. (Assumption that the first character is not a comma) For each character you read in, you write to a temp buffer. Once you hit the comma, convert the temp buffer string to an int, store the int on the array or list. Continue until you've hit the end of string, convert the last. Now you have an array of numbers that you can walk through and do work on them. Note: If all you wanted was to check if any of them is < X and walk through the list *only* once... You do the loop character by character, and you initialize a boolean flag set to false. When you parse each number, convert, you perform your logic check. If true, reset the flag to true. Then you can either continue or stop. At the end of the loop, you check the flag, true/false and then do your logic. Piece of cake. But hey! What do I know? Not a damn thing. I'm playing in the clouds... :-)