Newbie asks: How to translate this line of C to Py

James Logajan JamesL at Lugoj.Com
Tue Jul 10 01:19:44 EDT 2001


"Steve S.L. Wong" wrote:
> 
> while (command = strtok(line_ptr,";"),line_ptr=0,command) {
> }

I've been coding C for 20 years and I can therefore say authoritatively that
that is one ugly line of code good sir. You've really abused the comma
operator. Oh well, no animals were harmed I assume.

Anyway, assuming you are doing something with the tokens inside the while
loop, one equivalent appears to be:

import string

for command in filter(None, string.split(line_ptr, ";")):
	print command

The string.split() tokenizes line_ptr, but includes zero length tokens, so I
am applying the filter() function with the "identity" function None, which
will remove the non-true list elements (in this case the zero-length
strings) from the list of tokens.

Hope this helps.



More information about the Python-list mailing list