dbTalk Databases Forums  

looping through CSV list

comp.databases.informix comp.databases.informix


Discuss looping through CSV list in the comp.databases.informix forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
monkeys paw
 
Posts: n/a

Default looping through CSV list - 01-26-2011 , 07:56 AM






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

Reply With Quote
  #2  
Old   
Marco Greco
 
Posts: n/a

Default Re: looping through CSV list - 01-26-2011 , 08:50 AM






On 26/01/11 13:56, monkeys paw wrote:
Quote:
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

Reply With Quote
  #3  
Old   
monkeys paw
 
Posts: n/a

Default Re: looping through CSV list - 01-26-2011 , 10:08 AM



On 1/26/2011 9:50 AM, Marco Greco wrote:
Quote:

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.

Reply With Quote
  #4  
Old   
mpruet
 
Posts: n/a

Default Re: looping through CSV list - 01-26-2011 , 10:12 AM



On Jan 26, 7:56*am, monkeys paw <mon... (AT) joemoney (DOT) net> wrote:
Quote:
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;
}

Reply With Quote
  #5  
Old   
Marco Greco
 
Posts: n/a

Default Re: looping through CSV list - 01-26-2011 , 10:21 AM



On 26/01/11 16:08, monkeys paw wrote:
Quote:
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

Reply With Quote
  #6  
Old   
Ian Michael Gumby
 
Posts: n/a

Default RE: looping through CSV list - 01-26-2011 , 11:32 AM



Quote:
Date: Wed, 26 Jan 2011 16:21:35 +0000
From: marco (AT) 4glworks (DOT) com
To: informix-list (AT) iiug (DOT) org
Subject: Re: looping through CSV list


As others have pointed out, the question is - do you want to do it frominside
the engine, or do you want to do it from a client? If you want to do itfrom 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


He never did say it was in engine or not. Since the values are in a comma delimited string (he said CSV) it could be in a comma delimited file.

I saw Madison's response.
In C, why not use strtok and create tokens using the delimiter of ',' ?
Then you do an string to int conversion and then comparison.

Much easier.

And no, I'm not going to print out the code. That's left as an exercise of the user. (Hint: Google C and strtok example )

But hey! what do I know? ;-)

-G

Reply With Quote
  #7  
Old   
monkeys paw
 
Posts: n/a

Default Re: looping through CSV list - 01-26-2011 , 11:52 AM



On 1/26/2011 11:12 AM, mpruet wrote:
Quote:
On Jan 26, 7:56 am, monkeys paw<mon... (AT) joemoney (DOT) 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?

Reply With Quote
  #8  
Old   
Ian Michael Gumby
 
Posts: n/a

Default RE: looping through CSV list - 01-26-2011 , 01:51 PM



Quote:
Date: Wed, 26 Jan 2011 12:52:05 -0500
From: monkey (AT) joemoney (DOT) net
Subject: Re: looping through CSV list
To: informix-list (AT) iiug (DOT) 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 forthe 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 throughthe list *only* once...

You do the loop character by character, and you initialize a boolean flagset 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 yourlogic.

Piece of cake.

But hey! What do I know?

Not a damn thing. I'm playing in the clouds... :-)

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.