imoree Posted September 27, 2011 Posted September 27, 2011 Hey everyone; i am testing a script but after 1 iteration, i get stuck in infinite loop. I am lost for real. tried what little i know so far. here is code Set Variable [ $text; Value:"this is the name of my text" ] If [ Length ($text) > 1 ] Set Variable [ $text2; Value:$text ] Loop Set Variable [ $text2; Value:Left ( $text ; Length ( $text) - 1) ] Exit Loop If [ Length ( $text) = 1 ] End Loop Else End If Set Variable [ $text; Value:$text2 ] thanks -i
jbante Posted September 27, 2011 Posted September 27, 2011 Your Exit Loop If [] condition is based on the length of $text, but you aren't editing $text at all during the loop, only $text2: the Exit Loop If [] step is waiting for a condition that will never come unless you make $text shorter during the loop. What are you trying to accomplish with this script?
imoree Posted September 27, 2011 Author Posted September 27, 2011 Well i got this one to work. I had the $text variable being tested to iterate instead of the $text2 var @Jeremy. what i am testing is iteration vs recursion. Set Variable [ $text; Value:"this is the name of my text" ] If [ Length ($text) > 1 ] Set Variable [ $text2; Value:$text ] Loop Exit Loop If [ Length ( $text2) ≤ 0 ] Set Variable [ $text2; Value:Left ( $text ; Length ( $text2) - 1) ] End Loop Else End If - what this does is it takes 1 char off the entire string. Now - i have to try this doing recursion which is hurting my head. -ian Well i got this one to work. I had the $text variable being tested to iterate instead of the $text2 var @Jeremy. what i am testing is iteration vs recursion. Set Variable [ $text; Value:"this is the name of my text" ] If [ Length ($text) > 1 ] Set Variable [ $text2; Value:$text ] Loop Exit Loop If [ Length ( $text2) ≤ 0 ] Set Variable [ $text2; Value:Left ( $text ; Length ( $text2) - 1) ] End Loop Else End If - what this does is it takes 1 char off the entire string. Now - i have to try this doing recursion which is hurting my head. -ian Now, the headache -" RECURSION "__ DOH!!! JBante-- You really dont believe in spoon feeding hey? -LOL.. : ( Well i got this one to work. I had the $text variable being tested to iterate instead of the $text2 var @Jeremy. what i am testing is iteration vs recursion. - what this does is it takes 1 char off the entire string. Now - i have to try this doing recursion which is hurting my head. -ian Now, the headache -" RECURSION "__ DOH!!! JBante-- You really dont believe in spoon feeding hey? -LOL.. : ( Set Variable [ $text; Value:"this is the name of my text" ] If [ Length ($text) > 1 ] Set Variable [ $text2; Value:$text ] Loop Exit Loop If [ Length ( $text2) ≤ 0 ] Set Variable [ $text2; Value:Left ( $text ; Length ( $text2) - 1) ] End Loop Else End If THe Thing is. how to get the script to call itself without a "Sub-script" in order to avoid issues in debugging later on. MY Goodness, this logic is giving me a head ache.. Where is my advil ?? Use Exit Script ( Result: Length($text2) ) or ?? Or am i using recursion with a loop ? no right. because iteration is using a counter-like mechanism. DANG!!
doughemi Posted September 27, 2011 Posted September 27, 2011 What are you trying to accomplish with this script? What Jeremy said. You still haven't told us what the end result is supposed to be.
imoree Posted September 27, 2011 Author Posted September 27, 2011 I am not looking for an end result so much as to just test iteration & recursion. See how it works. - i
imoree Posted September 27, 2011 Author Posted September 27, 2011 Now the recursion part # -Name of --- recurse_Length Set Variable [ $$text; Value:Get(ScriptParameter) ] If [ Length ( $$text ) = 0 ] Exit Script [ Result: 1 ] Else Perform Script [ "recurse_Length "; Parameter: Left ($$text; Length ($$text) - 1 ) ] End If Any ideas - Again this is only testing an idea - I hope this looks right. I think i am getting it!! -ian
Vaughan Posted September 28, 2011 Posted September 28, 2011 I am not looking for an end result so much as to just test iteration & recursion. See how it works. Errm, recursion only works if there is a defined end result. Otherwise it's an infinite loop.
imoree Posted September 28, 2011 Author Posted September 28, 2011 ok I didnt think about that, but now what i am trying to do is restack my results I.e. "this is a test" --? change this to "test a is this" , but i want to do this using a script and recursion to see how it works. //Name of script:: = recurse_Length Set Variable [ $$text; Value:Get(ScriptParameter) ] If [ Length ( $$text ) = 0 ] Exit Script [ Result: 1 ] Else Perform Script [ “recurse_Length”; Parameter: Left ($$text; Length ($$text) - 1 ) ] //Set Variable [ $re; Value:Right ( Get(ScriptParameter) ; 1) ] #this does nothing really End If Show Custom Dialog [ Message: $re; Buttons: “OK”, “Cancel” ] // again i only see 1 -ian
Vaughan Posted September 28, 2011 Posted September 28, 2011 There are others far more knowledgeable with recursion, but my experience is that the function builds the result in a variable each iteration, then checks the exit condition and either returns the result or goes around again. Hence my post above about the infinite loop.
Kilyaaan Posted September 28, 2011 Posted September 28, 2011 Actually the script have an exit condition: If [ Length ( $$text ) = 0 ] Exit Script [ Result: 1 ] After this there are no more script calls. But what i didnt have was any output. Not even the Result: 1, this would be returned to the second last called script where there is no handling of script result so it would dissipate in to thin air, and then all the called scripts would finish processing the last End If statement, exiting at the first called script, with no content in ScriptResult. Correct me if im wrong but your next script example in 'I.e. "this is a test" --? change this to "test a is this" , but i want to do this using a script and recursion to see how it works.' Is the same script - but i guess thats why you made this post to learn recursion - I'll try to make a script that does what you describe. - you don't need to store an accumulating result in a variable, its often done in Filemaker scripts and Functions true, but each iteration could add their own piece of the total result, or the result from the last called script could be passed all the way back to the first called script, as ian would have needed to do if he wanted to see his '1' --- Her i made an example Notice that the variables used in the Recursion script are local variables, they only hold temporary information for each script call. Recursion.fp7.gz Set Variable [ $input; Value:Get ( ScriptParameter ) // notice we are only using normal script variables ] Set Variable [ $count; Value:WordCount ( $input ) ] Set Variable [ $last; Value:RightWords ( $input ; 1 ) // We grab the last word to put in front in the last script line ] If [ $count > 1 ] Set Variable [ $rest; Value:LeftWords ( $input ; $count - 1 ) // we pass on the rest of words on to the next calll ] Perform Script [ “Recurse words”; Parameter: $rest ] End If Exit Script [ Result: $last & " " & Get ( ScriptResult ) ]
imoree Posted September 29, 2011 Author Posted September 29, 2011 Kilyaaan ; This is perfect. I see how the script makes its calls now and where it takes the variable inputs as well as the outputs. Makes so much sense now. thank you for taking the time to do this.. -i
Recommended Posts
This topic is 4804 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now