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.

replacing words..?

Old posts that have not been replied to for several years.
Locked
s
slashem
Voice
Posts: 18
Joined: Tue Aug 13, 2002 6:35 pm

replacing words..?

Post by slashem »

Hiya..
I have a txtfile with on each line a few words like:
BLUB BLUB ONE TWO
BLUB OTHER WORDS
..
there are a few of these lines.. every odd line contains 2x BLUB (or another word twice), every even line 1x BLUB (or again another word).

I'm trying to make an mirc trigger that will rename BLUB BLUB into for example BLUB2 BLUB2

the trigger and the amount of arguments with the trigger is not a problem..
the problem is with lists and strings.
I can find the word BLUB in the lines but when I use lreplace to replace the words, the entire string becomes {BLUB2 BLUB2 ONE TWO}
<- adds those brackets..
when I use lappend it becomes BLUB2 BLUB2 {ONE TWO}

I'll pase a piece of the code here:

Code: Select all

   if {[lindex $list1 0] == "sname" } {
       set fs [open blub.txt r+]
       set found 0
       while {![eof $fs]} {
           set newstring ""
           gets $fs line
           set list2 [split $line]
           if { "[lindex $list2 0]" == "[lindex $list1 1]" } {
              incr found
              if { $found == "1" } {
                  lappend newstring [lindex $list1 2]
                  lappend newstring [lindex $list1 2]
                  lappend newstring [lrange $list2 2 [expr [llength $list2] -1] ]
                  putserv "PRIVMSG $chan :$newstring"
               } 
              if { $found == "2" } {
                lappend newstring [lreplace $list2 0 0 [lindex $list1 2]]
                #putserv "PRIVMSG $chan :[lreplace $list2 0 0 [lindex $list1 2]]"
                
              }
$list1 contains the vars of the trigger.. would be called from mirc with !set sname BLUB BLUB
$list2 contains a list of the words in the line that was read from the textfile.
I compare the word BLUB from the trigger with the first word from each line in the textfile ( if { "[lindex $list2 0]" == "[lindex $list1 1]" } { )
lindex $list2 0 = that first word..

since there are only two lines with that BLUB on it I used $found here with 0 as start value and 1, 2 for when it is found.. cuz the first time it needs to to two replacements, the second time only one.

the three lappend's for when $found == "1" could also be written as:

Code: Select all

                  lreplace $list2 [lindex $list2 0] [lindex $list1 1]
                  lreplace $list2 [lindex $list2 1] [lindex $list1 1] 
the main difference is: with lappend I get BLUB2 BLUB2 {ONE TWO}
with lreplace {BLUB2 BLUB2 ONE TWO}

NOW, I wanna get rid of these annoying brackets {} :)

I have to mention that I'm a total n00b with tcl.. just started today, but have learned a lot already today and it's cool:)

thx in advance to anyone who can and is willing to help me out

greetz
Slashem
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

That is because you are working with strings, but using list based commands. You should use [split] before using lreplace.

And [join] at the end, to turn the list back into a string.

As for your mIRC adiction, I would refer you to here
s
slashem
Voice
Posts: 18
Joined: Tue Aug 13, 2002 6:35 pm

Post by slashem »

thx for the tip..
used:

Code: Select all

              if { $found == "1" } {
                  set newstring [join [lreplace $list2 0 1 [lindex $list1 2] [lindex $list1 2]]]
                  putserv "PRIVMSG $chan :$newstring"
now, and that works :)
Locked