RE: looping through CSV list
Posted in 2011
Well I said strtok() as the starting place as a general hint. Since we don't have a use case, and we're not writing code here... strtok() puts the OP on the right path. As to mangling the string, if you want your initial string immutable... simply make a copy. We're treating the input as a string so yes, you will have a null at the end of the string. Which is why you then convert the string back to an integer. Again the advantage is that you can focus on the token in a more general sense. In OP's example, all numeric strings are of the same length. You could do a strcmp() to determine <> since the strings being compared are the same length and will then yield the correct result, or be more correct and make them integers prior to conversion. Again very fast and efficient. Now if you want to walk down the string on your own, copying characters in to a buffer and then convert buffer to an int... same thing, no destruction of the original string. (But a malloc()/calloc() memncpy() is pretty clean and simple too.) -G From: jonathan.leffler@gmail.com Date: Wed, 26 Jan 2011 09:43:36 -0800 Subject: Re: looping through CSV list To: informix-list@iiug.org On Wed, Jan 26, 2011 at 09:32, Ian Michael Gumby <im_gumby@hotmail.com> wrote: 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 ) Disquisition on the iniquities of strtok() - you can ignore this if you like (but at your own peril) Be wary of strtok(). If your code doesn't call any other functions that use strtok(), and if none of the functions that ever call your code is in the middle of using strtok(), then using strtok() is OK. If you cannot make both sets of guarantees (and library functions cannot), using strtok() yourself makes a mess of other places using it - it would be crucial for a library function to document that it uses strtok(), for example. You can often be reasonably confident about the functions you are calling; it is much harder to be confident about the functions that call yours. Clearly, if the strtok() is used in your main() program, you're likely to be OK - if your called functions are clean. Use strtok_r() where available. Use another algorithm altogether where you can't use strtok_r(). Note that strtok() and strtok_r() both mangle their input strings, placing nulls at the ends of tokens. That means you can't operate on string literals, etc. -- Jonathan Leffler <jonathan.leffler@gmail.com> #include <disclaimer.h> Guardian of DBD::Informix - v2008.0513 - http://dbi.perl.org "Blessed are we who can laugh at ourselves, for we shall never cease to be amused." _______________________________________________ Informix-list mailing list Informix-list@iiug.org http://www.iiug.org/mailman/listinfo/informix-list