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.

foreach problem

Old posts that have not been replied to for several years.
Locked
s
smds
Voice
Posts: 22
Joined: Mon Sep 15, 2003 6:11 pm

foreach problem

Post by smds »

keep getting this error:
wrong # args: should be "foreach varList list ?varList list ...? command"
while executing this code:

Code: Select all

proc something {nick mask hand chan arg} {
 set arg [split [join $arg] " "]
 set av ""
 set tl ""
 set flag [lindex $arg 0]
 set mode ""
 foreach cf [split $flag ""] {
  if {$cf == "+"} {
   set mode $cf
  }
  if {$cf == "-"} {
   set mode $cf
  }
  if {$mode == "+"} {
   switch -- $cf {
    "v" {
          code...
    }
    "t" {
          code...
    }
    }
   }
  } elseif {$mode == "-"} {
   switch -- $cf {
    "v" {
          code...
    }
    "t" {
          code...
    }
    }
   }
code...
}
im either going blind, or have been looking at it for too long,
just cant see the problem. rest of the code is fine, just the foreach
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

With proper indentation you'd notice the problem I think :P
Take a close look at your code:

Code: Select all

proc something {nick mask hand chan arg} { 
	set arg [split [join $arg] " "] 
	set av "" 
	set tl "" 
	set flag [lindex $arg 0] 
	set mode "" 
	foreach cf [split $flag ""] {
		if {$cf == "+"} { 
			set mode $cf 
		} 
		if {$cf == "-"} { 
			set mode $cf 
		} 
		if {$mode == "+"} { 
			switch -- $cf { 
				"v" { 
					code... 
				} 
				"t" { 
					code... 
				} 
			} 
		} 
	} elseif {$mode == "-"} { 
		switch -- $cf { 
			"v" { 
				code... 
			} 
			"t" { 
				code... 
			} 
		} 
	} 
	code... 
} 
Have you ever read "The Manual"?
s
smds
Voice
Posts: 22
Joined: Mon Sep 15, 2003 6:11 pm

Post by smds »

nope, still cant see it :oops:
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

smds wrote:nope, still cant see it :oops:
You need to understand how the tcl interpreter defines "words" or arguments passed to the commands to fully understand this. (read and understand this entire page: http://tcl.tk/man/tcl8.4/TclCmd/Tcl.htm )

You've misplaced a } (meant for one of your ifs) and made the 'elseif' part (including the word 'elseif') of that 'if' become arguments passed to foreach. In your code, 6 arguments are passed to 'foreach' and that doesn't work because foreach expects an odd number of arguments (pairs of variable names/values + a command).

Notice how foreach lines up with '} elseif {...' when proper indentation is applied. (my previous post)
Check those braces one more time.
Have you ever read "The Manual"?
s
smds
Voice
Posts: 22
Joined: Mon Sep 15, 2003 6:11 pm

Post by smds »

Had a look over i again and again, and still dont see the problem,
i did add another close brace to close that if, but then got
an error about '"chan" no such var'
this code isnt mine anyway, so i dont fully understand it
i know the basics, but heres the code as i have it un edited.

Code: Select all

proc chanflags {nick mask hand chan arg} {
 set arg [split [join $arg] " "]
 set av ""
 set tl ""
 set flag [lindex $arg 0]
 set mode ""
 foreach cf [split $flag ""] {
  if {$cf == "+"} {
   set mode $cf
  }
  if {$cf == "-"} {
   set mode $cf
  }
  if {$mode == "+"} {
   switch -- $cf {
    "v" {
 code...
    }
    "t" {
 code...
    }
    }
   }
  } elseif {$mode == "-"} {
   switch -- $cf {
    "v" {
 code...
    }
    "t" {
 code...
    }
    }
   }
  }
 }
 if {[string match *+voiceall* [channel info $chan]]} {set av "v"}
 if {[string match *+topiclock* [channel info $chan]]} {set tl "t"}
 if {$flag == ""} {puthelp "NOTICE $nick :chanflags: $tl$av"}
}
Locked