429,570 Members | 999 Online
Need help? Post your question and get tips & solutions from a community of 429,570 IT Pros & Developers. It's quick & easy.

# need help translating a PHP for statement to python

 P: n/a hi; I'm translating some PHP scripts to Python and have hit a roadblock with a for statement. If someone could explain to me how one should translate the multiple increment, evaluations, etc. in the first line I would appreciate it deeply ... for (\$y = 0, \$x = \$cx-\$cy-1; \$y <= \$cy; ++\$y,++\$x) { \$prd = \$q * \$y_ar[\$y] + \$car; \$prd -= (\$car = intval(\$prd / 1E7)) * 1E7; if (\$bar = ((\$x_ar[\$x] -= \$prd + \$bar) < 0)) \$x_ar[\$x] += 1E7; } thanks, Davis Jul 18 '05 #1
6 Replies

 P: n/a dm*@machinic.net (Davis Marques) writes: I'm translating some PHP scripts to Python and have hit a roadblock with a for statement. If someone could explain to me how one should translate the multiple increment, evaluations, etc. in the first line I would appreciate it deeply ... for (\$y = 0, \$x = \$cx-\$cy-1; \$y <= \$cy; ++\$y,++\$x) { \$prd = \$q * \$y_ar[\$y] + \$car; \$prd -= (\$car = intval(\$prd / 1E7)) * 1E7; if (\$bar = ((\$x_ar[\$x] -= \$prd + \$bar) < 0)) \$x_ar[\$x] += 1E7; } Python is more verbose than PHP when it comes to stuff like that. You have to expand things: x = cx - cy - 1 for y in xrange(cy+1): prd = q * y_ar[y] + car; car = intval(prd / 1E7) prd -= car * 1E7; x_ar[x] -= prd + bar bar = (x_ar[x] < 0) # is this REALLY what you wanted? if bar: x_ar[x] += 1E7; x += 1 You can more closely approximate the two parallel loops with the iterzip function in the new itertools package, but that only works with the latest Python versions. I suspect that your if statement was actually supposed to say if ((\$bar = (\$x_ar[\$x] -= \$prd + \$bar)) < 0) \$x_ar[\$x] += 1E7; but I don't know what you're really trying to do. Jul 18 '05 #2

 P: n/a Haha ... you must be originally a perl coder. Who else writes attricious code like this if (\$bar = ((\$x_ar[\$x] -= \$prd + \$bar) < 0)) If I was hiring you, I'd fire you on the spot. Secondly, here is what I came up with. Not sure what '1E7' is though. And change 'intval' to 'int' if you want to cast something to an int in python x,y = cx-cy-1,0 while y <= cy: prd = q * y_ar[y] + car - ((car = int(prd / 1E7)) * 1E7); if (bar = ((x_ar[x] -= prd + bar) < 0)) x_ar[x] += 1E7; x,y = x+1,y+1 Eli I'm translating some PHP scripts to Python and have hit a roadblock with a for statement. If someone could explain to me how one should translate the multiple increment, evaluations, etc. in the first line I would appreciate it deeply ... for (\$y = 0, \$x = \$cx-\$cy-1; \$y <= \$cy; ++\$y,++\$x) { \$prd = \$q * \$y_ar[\$y] + \$car; \$prd -= (\$car = intval(\$prd / 1E7)) * 1E7; if (\$bar = ((\$x_ar[\$x] -= \$prd + \$bar) < 0)) \$x_ar[\$x] += 1E7; } thanks, Davis Jul 18 '05 #3

 P: n/a Paul Rubin wrote: Python is more verbose than PHP when it comes to stuff like that. marques = """for (\$y = 0, \$x = \$cx-\$cy-1; \$y <= \$cy; ++\$y,++\$x) { \$prd = \$q * \$y_ar[\$y] + \$car; \$prd -= (\$car = intval(\$prd / 1E7)) * 1E7; if (\$bar = ((\$x_ar[\$x] -= \$prd + \$bar) < 0)) \$x_ar[\$x] += 1E7; }""" rubin = """x = cx - cy - 1 for y in xrange(cy+1): prd = q * y_ar[y] + car car = int(prd / 1E7) prd -= car * 1E7 x_ar[x] -= prd + bar bar = x_ar[x] < 0 if bar: x_ar[x] += 1E7 x += 1""" if __name__ == "__main__": print "The ultimate verbosity check :-)" print "PHP", len(marques), "characters" print "Python", len(rubin), "characters" Output: The ultimate verbosity check :-) PHP 206 characters Python 205 characters Note that I didn't cheat and let if bar: x_ar[x] += 1E7 spread over two lines, thus deliberately wasting another 8 characters. Peter Jul 18 '05 #4

 P: n/a In article , Eli Pollak wrote: Haha ... you must be originally a perl coder. Who else writes attricious code like this if (\$bar = ((\$x_ar[\$x] -= \$prd + \$bar) < 0)) I know, I know! C programmers! Right? -- ..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:. : d r i n k i n g l i f e o u t o f t h e c o n t a i n e r : Jul 18 '05 #5

 P: n/a On Sat, 10 Jan 2004 20:22:18 +1100, "Eli Pollak" wrote: Haha ... you must be originally a perl coder. Who else writes attriciouscode like this if (\$bar = ((\$x_ar[\$x] -= \$prd + \$bar) < 0)) Any C programmer worth his salt. I miss those days. Sort of. --dang Jul 18 '05 #6

 P: n/a Dang Griffith writes:Haha ... you must be originally a perl coder. Who else writes attriciouscode like this if (\$bar = ((\$x_ar[\$x] -= \$prd + \$bar) < 0)) Any C programmer worth his salt. I miss those days. Sort of. But that line almost certainly has the parentheses in the wrong place. Look carefully. Jul 18 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.