piece function - there must be an easier/more elegant way !?
- Posted by freeplay at mailandnews.com May 14, 2001
- 351 views
This is the preamble of a multipart MIME formatted message. If you are reading this text your mail system is most likely not capable of properly decoding MIME messages. To extract the contents of this message, save it to a file and then use an external MIME decoding utility. --mime-boundary-interchange-3b364e84 Hello folks, Attached is a short text file containing a function I've called piece. It takes three arguments, a string sequence, a delimiter character and an integer index. Using the delimter character it chops the string into pieces and returns the one specified by the integer index. The integer index begins at 1. Hence: sequence s s = "andy:1:hello there::ok" printf(1, "%s", {piece(s, ':', 1)}) gives "andy" printf(1, "%s", {piece(s, ':', 2)}) gives "1" printf(1, "%s", {piece(s, ':', 3)}) gives "hello there" and printf(1, "%s", {piece(s, ':', 4)}) gives the null string "". Asking for a piece out of range will also give "". I think the way I've coded this is rather bad - there must be a more elegant solution. Any offers? I've thought of strtok.e in the archives but it might be a little overkill for my needs. Note I needed to put the code in an attached text file as my web based mailer messes up code indentation something rotton. Best regards, FP. --mime-boundary-interchange-3b364e84 Content-Type: text/plain; name="piece.txt" Content-disposition: attachment; filename="piece.txt" Content-Transfer-Encoding: BASE64