使用递归通过自己的命令在循环中创建循环
我在递归方面遇到了一些困难。 我有一个 .txt 文件,其中包含这样的命令(<是注释):
< HEADER 1
< HEADER 2
DO STH HERE
LOOP lblA 2
DO STH
DO STH ELSE
END lblA
DO STH HERE
如果有“LOOP lbl x”命令,则该工具必须重复该行和“END lbl”之间的每个命令 x 次。在通过 TCP 将每一行发送到服务器之前,输出会被临时保存:
< HEADER 1
< HEADER 2
DO STH HERE
< LOOP lblA 1 of 2
DO STH
DO STH ELSE
< LOOP lblA 2 of 2
DO STH
DO STH ELSE
< END LOOP lblA
DO STH HERE
这很简单。但现在我想要循环中的循环,例如
< HEADER 1
< HEADER 2
DO STH HERE
LOOP lblA 2
DO STH
LOOP lblB 3
print("nope")
END lblB 3
DO STH ELSE
END lblA
DO STH HERE
输出必须是
< HEADER 1
< HEADER 2
DO STH HERE
< LOOP lblA 1 of 2
DO STH
< LOOP lblB 1 of 3
print("nope")
< LOOP lblB 2 of 3
print("nope")
< LOOP lblB 3 of 3
print("nope")
< END LOOP lblB
DO STH ELSE
< LOOP lblA 2 of 2
DO STH
< LOOP lblB 1 of 3
print("nope")
< LOOP lblB 2 of 3
print("nope")
< LOOP lblB 3 of 3
print("nope")
< END LOOP lblB
DO STH ELSE
< END LOOP lblA
DO STH HERE
这是我到目前为止的代码:
def comTester(self, testfile):
rawtext = Path(testfile).read_text()
txt = rawtext.split('\n')
for i in range (0, len(txt), 1):
loopArray = []
loopLineA = 0
loopLineB = 0
loopLbl = ''
loops = 0
j = 0
k = 1
if txt[i].startswith('<') == True:
testcommands.append(txt[i])
elif txt[i].startswith('LOOP') == True:
loopArray = txt[i].split(' ')
loopLineA = i + 1
loopLbl = loopArray[1]
loops = int(loopArray[2])
for j in range (loopLineA, len(txt), 1):
if txt[j].startswith('END ' + loopLbl) == True:
loopLineB = j
while k < loops:
l = 0
testcommands.append('< LOOP "' + loopLbl + str(k) + ' of ' + str(loops))
for l in range(loopLineA, loopLineB, 1):
testcommands.append(txt[l])
k = k + 1
testcommands.append('# END LOOP ' + loopLbl)
elif txt[i].startswith('END') == True:
continue
else:
testcommands.append(txt[i])
return
我需要启动“elif txt[i].startswith('LOOP') == True:”块递归,但我不这样做知识。我必须创建一个新函数并调用它本身。但对于这个给定的结构......没有任何线索。
I'm struggling a little with recursion.
I have a .txt file with commands like this (< are comments):
< HEADER 1
< HEADER 2
DO STH HERE
LOOP lblA 2
DO STH
DO STH ELSE
END lblA
DO STH HERE
If there is a "LOOP lbl x" command, the tool have to repeat every command between this line and the "END lbl" x times. The output is saved temporarily before sending each line via TCP to a server:
< HEADER 1
< HEADER 2
DO STH HERE
< LOOP lblA 1 of 2
DO STH
DO STH ELSE
< LOOP lblA 2 of 2
DO STH
DO STH ELSE
< END LOOP lblA
DO STH HERE
That is easy. But now I want loops in loops like
< HEADER 1
< HEADER 2
DO STH HERE
LOOP lblA 2
DO STH
LOOP lblB 3
print("nope")
END lblB 3
DO STH ELSE
END lblA
DO STH HERE
The output must be
< HEADER 1
< HEADER 2
DO STH HERE
< LOOP lblA 1 of 2
DO STH
< LOOP lblB 1 of 3
print("nope")
< LOOP lblB 2 of 3
print("nope")
< LOOP lblB 3 of 3
print("nope")
< END LOOP lblB
DO STH ELSE
< LOOP lblA 2 of 2
DO STH
< LOOP lblB 1 of 3
print("nope")
< LOOP lblB 2 of 3
print("nope")
< LOOP lblB 3 of 3
print("nope")
< END LOOP lblB
DO STH ELSE
< END LOOP lblA
DO STH HERE
This is my code so far:
def comTester(self, testfile):
rawtext = Path(testfile).read_text()
txt = rawtext.split('\n')
for i in range (0, len(txt), 1):
loopArray = []
loopLineA = 0
loopLineB = 0
loopLbl = ''
loops = 0
j = 0
k = 1
if txt[i].startswith('<') == True:
testcommands.append(txt[i])
elif txt[i].startswith('LOOP') == True:
loopArray = txt[i].split(' ')
loopLineA = i + 1
loopLbl = loopArray[1]
loops = int(loopArray[2])
for j in range (loopLineA, len(txt), 1):
if txt[j].startswith('END ' + loopLbl) == True:
loopLineB = j
while k < loops:
l = 0
testcommands.append('< LOOP "' + loopLbl + str(k) + ' of ' + str(loops))
for l in range(loopLineA, loopLineB, 1):
testcommands.append(txt[l])
k = k + 1
testcommands.append('# END LOOP ' + loopLbl)
elif txt[i].startswith('END') == True:
continue
else:
testcommands.append(txt[i])
return
I need to start the "elif txt[i].startswith('LOOP') == True:" block recursive, but I don't know how. I have to create a new function and calling it itself. But with this given structure... no clue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论