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.

lsearch help

Help for those learning Tcl or writing their own scripts.
Post Reply
m
mrNobody
Voice
Posts: 14
Joined: Mon Dec 02, 2013 4:49 am

lsearch help

Post by mrNobody »

Okay, I'm almost afraid to make yet another topic :$ I used search and couldn't find an answer, so here we go:

I have this piece of code:

Code: Select all

set family {
	pubchan #chan1
	famchan #chan2
	capochan #chan3
	topchan #chan4
}

proc test {nick host handle chan args} {
	global family
	set family [split [string trim $family] "\n"]
	set pubchan [lindex [lsearch -inline $family "pubchan* *"] 1]
	set famchan [lindex [lsearch -inline $family "famchan* *"] 1]
	set capochan [lindex [lsearch -inline $family "capochan* *"] 1]
	set topchan [lindex [lsearch -inline $family "topchan* *"] 1]
	putserv "PRIVMSG $chan :pubchan is $pubchan, famchan is $famchan, capochan is $capochan, topchan is $topchan"
}
Now that only works for pubchan, everything else returns an empty string. How can I get all the values I want?
w
willyw
Revered One
Posts: 1207
Joined: Thu Jan 15, 2009 12:55 am

Re: lsearch help

Post by willyw »

mrNobody wrote: ...
How can I get all the values I want?

Experiment with:

Code: Select all


set family [list {pubchan #chan1} {famchan #chan2} {capochan #chan3} {topchan #chan4} ]


bind pub - "!mrnobody" mrnobody


proc mrnobody {nick host handle chan args} {
global family

        set pubchan [lindex [lsearch -inline $family "pubchan* *"] 1]
        set famchan [lindex [lsearch -inline $family "famchan* *"] 1]
        set capochan [lindex [lsearch -inline $family "capochan* *"] 1]
        set topchan [lindex [lsearch -inline $family "topchan* *"] 1]
        putserv "PRIVMSG $chan :pubchan is $pubchan, famchan is $famchan, capochan is $capochan, topchan is $topchan"


     }
I hope this helps.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Code: Select all

set family { 
   pubchan #chan1 
   famchan #chan2 
   capochan #chan3 
   topchan #chan4 
} 

proc test {nick host handle chan args} { 
   global family 
   set fam [split [string trim $family] "\n"] 
   set pubchan [lindex [split [string trim [lsearch -inline $fam "*pubchan *"]]] 1] 
   set famchan [lindex [split [string trim [lsearch -inline $fam "*famchan *"]]] 1] 
   set capochan [lindex [split [string trim [lsearch -inline $fam "*capochan *"]]] 1] 
   set topchan [lindex [split [string trim [lsearch -inline $fam "*topchan *"]]] 1] 
   putserv "PRIVMSG $chan :pubchan is $pubchan, famchan is $famchan, capochan is $capochan, topchan is $topchan" 
} 
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
m
mrNobody
Voice
Posts: 14
Joined: Mon Dec 02, 2013 4:49 am

Post by mrNobody »

thnx spike, works flawlessly :)

I'm learning a lot of tcl principles by looking at your beer.tcl script, but the lack of comments makes it difficult to decipher sometimes xD
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Another option..

Code: Select all

set family {
   pubchan #chan1
   famchan #chan2
   capochan #chan3
   topchan #chan4
}

proc test {nick host handle chan text} {
  array set fam $::family
  puthelp "PRIVMSG $chan :pubchan is $fam(pubchan), famchan is $fam(famchan), capochan is $fam(capochan), topchan is $fam(topchan)"
}
NML_375
m
mrNobody
Voice
Posts: 14
Joined: Mon Dec 02, 2013 4:49 am

Post by mrNobody »

oh very interesting nml! I'm gonna look into that as well.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Worth mentioning,
array set expects a valid list of key-value pairs as input. For a trivial case, you'll be able to craft lists by hand; though I generally recommend using the list command for building more complex lists. This is especially important if you have strings containing spaces or brackets/braces.

Code: Select all

set family [list pubchan "#chan1 here" \
  famchan "#chan2 {there}" \
  capochan #otherchan \
  topchan "We're on the top #chan" \
]
...
NML_375
m
mrNobody
Voice
Posts: 14
Joined: Mon Dec 02, 2013 4:49 am

Post by mrNobody »

could anyone tell me how I can bind all those chans to an onjoin event?

I tried

Code: Select all

set famchans [split [string trim $family] "\n"]
foreach famchan $famchans {
  if {[llength $famchan]>"1"} {
    bind join - "[lindex $famchan 1] *" fambot:onjoin
  }
}
but that doesn't replace the [lindex $famchan 1] and crashes the bot
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

inspired by beer.

Post by SpiKe^^ »

mrNobody wrote:I'm learning a lot of tcl principles by looking at your beer.tcl script, but the lack of comments makes it difficult to decipher sometimes xD
Nice to hear you gather inspiration from my beer:)

Sorry there are few comments in the script, but at least it's short.
I would be glad to answer any specific questions you have about beer.
You can always contact me here, or at the contact info in beer.tcl
Last edited by SpiKe^^ on Sat Dec 21, 2013 3:25 pm, edited 2 times in total.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Maybe try this...

Code: Select all

proc doJoinBinds {} {
   foreach line [split [string trim $::family] "\n"] {
      bind join - "[lindex [split [string trim $line]] 1] *" fambot:onjoin
   }
}
doJoinBinds

SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Code: Select all

proc doJoinBinds {} {
  # unbind any existing onjoin binds #
  foreach jbind [binds fambot:onjoin] {
    foreach {typ flg name hit prc} $jbind {  break  }
    unbind $typ $flg $name $prc
  }

  # bind back the current 'family' channels #
  foreach line [split [string trim $::family] "\n"] {
    bind join - "[lindex [split [string trim $line]] 1] *" fambot:onjoin
  }
}
doJoinBinds

SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Post Reply