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.

lreplace and lsearch combination

Old posts that have not been replied to for several years.
Locked
B
BiLL
Halfop
Posts: 78
Joined: Wed Sep 26, 2001 8:00 pm
Location: Germany

lreplace and lsearch combination

Post by BiLL »

Hi,

I got a problem with lreplace and lsearch :(. I tried a few ways but I don't get it working correctly!


I got his:

msg = Hello Guys. Blablabla bla bla IRC bla bla bla bla.

I want to replace in $msg that IRC with $nick (current nick of the triggerer).

So I tried this one:

set msg [lreplace $msg [lsearch $msg "IRC"] [lsearch $msg "IRC"] $nick]

But then he put's $nick in FRONT of the line instead of replacing IRC with $nick :(.

Can someone help me please?
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

My guess is that the element "IRC" is not found, so 'lsearch' returns "-1" and that's where your nick ends up :) (remember: it's case sensitive)
Also make sure your $msg is a list before using list commands on it

Code: Select all

set msg [split $msg]
and then

Code: Select all

set msg [join $msg]
if you want it output as a string later on.
Have you ever read "The Manual"?
B
BiLL
Halfop
Posts: 78
Joined: Wed Sep 26, 2001 8:00 pm
Location: Germany

Post by BiLL »

Hi,

thanks for your answer.

set msg [split $msg]
set msg [lreplace $msg [lsearch $msg "IRC"] [lsearch $msg "IRC"] $nick]
set msg [join $msg]

I tried this code now but it's still the same. IRC is still not beeing replaced and my irc-nick lands in front of $msg :(.

Any other idea??
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

BiLL wrote:IRC is still not beeing replaced and my irc-nick lands in front of $msg :(.
Check if the element is found before you do the replacement (like my lsearch of [channels] in our previous thread)
'lsearch' returns -1 when what you search for is not found in the list and that's the only thing that could cause this behaviour. Again: check the case of that word in your list.. are you sure it's in all uppercase like the element you search for?
Have you ever read "The Manual"?
B
BiLL
Halfop
Posts: 78
Joined: Wed Sep 26, 2001 8:00 pm
Location: Germany

Post by BiLL »

Ah, I found the bug :)
Hehe.


Its because I am using that msg:

greetings, please query NICK, this is my msg.

Its because I am using NICK, <- if i leave the , it works. With , it doenst work. You maybe can help me with that also? So I can replace NICK, with my IRCNICK, ? There should be a way!
B
BiLL
Halfop
Posts: 78
Joined: Wed Sep 26, 2001 8:00 pm
Location: Germany

Post by BiLL »

please someone help me i don't know how to fix that :-(
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

BiLL wrote:Its because I am using NICK, <- if i leave the , it works. With , it doenst work. You maybe can help me with that also?
NICK is not NICK, in a list

here's a better proc (and no, it's not by me :D)

Code: Select all

proc varrep {string what with} {
	if {![string match "*$what*" $string]} {return $string}
	set cutstart [expr [string first $what $string] - 1]
	set cutstop  [expr $cutstart + [string length $what] + 1]
	set string [string range $string 0 $cutstart]$with[string range $string $cutstop end]
	return $string
}
set msg [varrep $msg "nick" $::botnick]
B
BiLL
Halfop
Posts: 78
Joined: Wed Sep 26, 2001 8:00 pm
Location: Germany

Post by BiLL »

great that works perfect thanks!
Locked