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.
Help for those learning Tcl or writing their own scripts.
sdays
Halfop
Posts: 98 Joined: Sat Oct 21, 2006 4:46 am
Post
by sdays » Thu Apr 19, 2007 9:07 pm
when my bot add a line to a text file i need it to add a num like below.
like:
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Thu Apr 19, 2007 9:12 pm
Show us your code.
sdays
Halfop
Posts: 98 Joined: Sat Oct 21, 2006 4:46 am
Post
by sdays » Thu Apr 19, 2007 9:19 pm
Code: Select all
proc addtemp {nick uhost} {
global tempfile file
set file [open $tempfile a]
puts $file "$nick $uhost"
close $file
return 0
}
sdays
Halfop
Posts: 98 Joined: Sat Oct 21, 2006 4:46 am
Post
by sdays » Fri Apr 20, 2007 7:49 pm
i need it to count the line before adding ,like add 1 then the line_here
sdays
Halfop
Posts: 98 Joined: Sat Oct 21, 2006 4:46 am
Post
by sdays » Sat Apr 21, 2007 5:58 am
ok heres what i get now "<eggdrop> Tcl error [temp_add]: channel "file12" wasn't opened for reading
Code: Select all
set temp "templist.txt"
proc addtemp {} {
global file linecount temp nick
set addtemp_file [open $temp w]
set linecount 0
set lines [split [read $addtemp_file] \n]
foreach line $lines {
incr linecount
}
puts $addtemp_file "$linecount $nick"
close $addtemp_file
return 0
}
nml375
Revered One
Posts: 2860 Joined: Fri Aug 04, 2006 2:09 pm
Post
by nml375 » Sat Apr 21, 2007 7:34 am
You're opening the file in write-only mode. Check your "access" argument for your
open -command. Best approach would be to first open your file as readonly, read all the data, close it, reopen it as write only (with truncation) and write the modified data.
NML_375
sdays
Halfop
Posts: 98 Joined: Sat Oct 21, 2006 4:46 am
Post
by sdays » Sat Apr 21, 2007 4:43 pm
i done that now other bug..
heres tcl:
Code: Select all
proc addtemp {} {
global linecount temp
set addtemp_file [open $temp a+]
set lines [split $addtemp_file "\n"]
foreach line $lines {
incr linecount
}
set linecount 0
puts $addtemp_file "$linecount $nick"
close $addtemp_file
return 0
}
here's text file:
nml375
Revered One
Posts: 2860 Joined: Fri Aug 04, 2006 2:09 pm
Post
by nml375 » Sat Apr 21, 2007 6:41 pm
It would seem you reset linecount to 0 after you've read all the lines in the file. Also, rather than using foreach to increase the count, you might considder using llength (since you already split it to a list) instead.
NML_375
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Sat Apr 21, 2007 7:43 pm
+ You're not reading your file. Try
Code: Select all
proc addtemp {} {
global temp
set addtemp_file [open $temp a+]
set lines [split [read $addtemp_file] \n]
set linecount [llength $lines]
puts $addtemp_file "[expr {$linecount+1}] $nick"
close $addtemp_file
return 0
}
sdays
Halfop
Posts: 98 Joined: Sat Oct 21, 2006 4:46 am
Post
by sdays » Sat Apr 21, 2007 7:55 pm
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Sat Apr 21, 2007 9:20 pm
I doubt you got any result at all because $nick does not exist. Perhaps this is better:
Code: Select all
proc addtemp nick {
global temp
set addtemp_file [open $temp r+]
set linecount [llength [split [read $addtemp_file] \n]]
puts $addtemp_file "$linecount $nick"
close $addtemp_file
return 0
}
usage: addtemp <nick> (this is supposed to continue the count, so we assume that the pevious nicks are already numbered in the file).