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 loops help

Old posts that have not been replied to for several years.
Locked
D
Dw2k
Voice
Posts: 16
Joined: Sat Dec 07, 2002 4:40 pm
Location: Dundee || Liverpool
Contact:

foreach loops help

Post by Dw2k »

I have a silly problem, with both my code and my head :D
My head doesn't understand loops in loops, and neither does my bot, as the code is wrong, anyway I digress...

Code: Select all

proc cmd:opstatus {nick uhost hand chan args} {
	global botnick hubchan userchan
	
	set message ""
	
	foreach channel [channels] {
		if {[onchan $botnick $channel]} {
			if {![botisop $channel]} {
				foreach user [chanlist $channel] {
					if {![isop $user $channel]} {
						set message "$message \0034$channel\003"
						break
					} elseif {[isop $user $channel]} {
						set message "$message \00310$channel\003"
						break
					} 
				}
			}
		}
	}
	if {$message != ""} {
		log "opstatus\: $message"
	}
}
This is a small scanner that should look at each channel the bot is on and see if it is deop'ed, and if it is, is there any other user oped? It prints out the name of the channel in different colours if there are other ops or if there arent.

Problem being that is that it ignores this logic and prints out the \0034 line regardless if there are op's or not.

</long post>

Thanks for any help :)
Dave
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

The logic is just a bit misplaced.

First loop: go through all the channels. This part is correct.

Second loop: go through each user in the current user.

If the user is not an op, print that message and break.
Otherwise, print the other message and break.

As you can see, the 2nd loop is only executed once, no matter what -- it just prints out one of the messages, depending on the first user it looks at.

You can probably figure it out now. Or ask again if you need more help.
D
Dw2k
Voice
Posts: 16
Joined: Sat Dec 07, 2002 4:40 pm
Location: Dundee || Liverpool
Contact:

Post by Dw2k »

Yeah thanks. Got it now with a bit a help and poke in the right direction.

My logic was never good :D
Dave
Locked