Using split to process a list

I have a variable that contains a comma seperated listing of between 1 and 8 tanks. I want to run an SQL proceedure for each one. I know I should be able to use split() and loop through the tanks from the list running the proceedure for each one, but I am having difficulty doing it.

Unless you just want moral support, you're going to have to elaborate :stuck_out_tongue_winking_eye:

Sorry.

[code]tday = fpmi.db.dateFormat(event.source.parent.parent.dtSelectedDate,“yyyy-MM-dd H:mm:ss”)
tankToList = “803, 804, 914”

for each tank in tankToList
fpmi.db.runupdateQuery(“exec usp.updateTanks ‘%s’,’%s’” % (tank,tday)[/code]

I know the syntax is not correct, but I think it will give you an idea of what I am trying to accomplish. I want to run this SQL procedure for each tank in the list passing it the new tank number and date.

Ron,

Try the following:

import string tday = fpmi.db.dateFormat(event.source.parent.parent.dtSelectedDate,"yyyy-MM-dd H:mm:ss") tankToList = "803, 804, 914" NewtankToList = string.split(tankToList,',') for each tank in NewtankToList: fpmi.db.runupdateQuery("exec usp.updateTanks '%s','%s'" % (tank,tday)

Almost there. The newTankToList has the list of tanks in it, but I get an error on the for loop:

import string
	Tday = fpmi.db.dateFormat(event.source.parent.parent.dtSelectedDate,"yyyy-MM-dd H:mm:ss")
	NewtankToList = string.split(tankToList,",")
	print NewtankToList
	for each tank in NewtankToList:
		print tank

Won’t even let me close the code window. It generates the error:

"Unable to register action actionPerformed
 Parse Error:
Traceback(innermost last):
(no code object) at line 0
SyntaxError: ('invalid syntax',",29,18,'\tfor each tank in NewtankToList:'))

Hi Ron,

Sorry, my mistake - there is no “each” keyword. The for loop should be

for tank in NewtankToList:

Just to check, you shouldn’t be indenting the lines after ‘import string’ - the only line you should be indenting is the last one, as it is in the for loop.

That was it. Thanks so much!!