This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

[SOLVED] return/continue

Help for those learning Tcl or writing their own scripts.
Post Reply
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

[SOLVED] return/continue

Post by raider2k »

i need some help or hint again :)

this time its about return 0/return 1 or something else like that. until now i used to use return 0 if i wanted a script to end but sometimes i only want to escape from the if and continue with the rest of the script. is there a way to do that?

in example:

Code: Select all

f { $chan == "#chan1" } {
	if { $nick == "notwantednick" } {
		fail
		escape that if and continue with if2, dont stop parsing the rest of the script
	}
}
if2 { ... } {
	code here
}
i hope i got it all right by explaining so you guys know what im after ^^

edit:

i might also want to add that i tried playing around with break, return 0, return 1 and continue
Last edited by raider2k on Wed May 20, 2009 1:37 pm, edited 1 time in total.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Don't use either...

When using if, the first parameter is the conditional to test, the second the code to execute if the conditional was true. The optional third would be what to execute if the conditional was false. There is no need to "escape" from those code-blocks.. once they reach the end, the processing continues with the first command after the if conditional.

Code: Select all

...
if {$chan == "#chan1"} {
  if {$nick == "notwantednick"} {
    #executed if $chan == #chan1 and $nick == notwantednick
  } {
    #executed if $chan == #chan1 and $nick != notwantednick
  }
}
if {$::botnick == "nisse"} {
  #executed if bot's nick is nisse, regardless of $chan or $nick...
}
Notice how I don't use any return, break, or continue there. Instead I let each code-block run to it's end.

Return has the sole purpose of stopping a proc from running, and returning a value to whatever called the proc in the first place. Whether you use 0 or 1 or anything else won't affect this behaviour, some eggdrop bindings do care for the returned value, but that's outside the scope of this discussion.

Continue is used in loops, such as for, foreach, and while. It instructs the loop controller to halt the current iteration immediately, and proceed to the next iteration.

Break is used in loops, such as for, foreach, and while, as well as the conditional construct switch It instructs the controller to halt the current iteration immediately, and return, allowing the next command in the script to be executed.
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

nml375 wrote:

Code: Select all

...
if {$chan == "#chan1"} {
  if {$nick == "notwantednick"} {
    #executed if $chan == #chan1 and $nick == notwantednick
  } {
    #executed if $chan == #chan1 and $nick != notwantednick
  }
}
if {$::botnick == "nisse"} {
  #executed if bot's nick is nisse, regardless of $chan or $nick...
}
Notice how I don't use any return, break, or continue there. Instead I let each code-block run to it's end.
alright, was thinking of an answer that would tell me to do it otherwise.
so theres no way like a goto or similar?

because sometimes i need the current if or a certain part to end and need another one to continue - as it happens sometimes in pubm's
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Pretty much any goto-logics can be replaced with proper conditional blocks..

Either by having consecutive conditionals, nested conditionals, or both.

Ie:

Code: Select all

if {case1} {
 puts stdout "case1 is true"
 if {case2} {
  puts stdout " in addition, case2 is also true"
 }
 puts stdout "end of case1 block
}
This would be a perfectly fine replacement for the pseudo-code below:

Code: Select all

if {case1} {
 puts stdout "case1 is true"
 if {not case2} {
  goto end
 }
 puts stdout " in addition, case2 is also true"

 end: puts stdout "end of case1 block"
}
Edit: And no, there is no "goto" function in tcl
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

well thanks
im sure im going to figure it out though how to handle it the way i want it to be in combination with ur advices ;)
Post Reply