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.

Deleting names in files

Old posts that have not been replied to for several years.
Locked
D
Dizzle
Op
Posts: 109
Joined: Thu Apr 28, 2005 11:21 am
Contact:

Deleting names in files

Post by Dizzle »

Hello, i have a little question.

The script im working on saves channels names too a file,
When the script its doing its job, it saves channels names too a files (writing) and it checkes of the channel name is already in there.

Butt my problem is how too delete a channelname, he has too search for it in a file that may contain like 100 channelnames and delete the one matching the channelname i like too erase from the file.

I have read suninet.nl for basic file working, butt the supject doenst coem forward in that Guide.

You guys have a Guide for me, or just the code you gave too use for this

Verry much thx's 8)
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

This proc should delete the name only if it exists:

Code: Select all

proc rem:chan {arg} {
 global file
 set exists 0
 set chan [lindex [split $arg] 0]
 foreach temp [split [string tolower [read [set inf [open $file]]]] "\n"][close $inf] {
  if {[string equal -nocase [lindex [split $temp] 0] $chan]} { 
   set exists 1
   break
  }
 }
 if {$exists} {
  set z ""
  set a [open $file r]
  while {![eof $a]} {
   set b [gets $a]
   if {![string equal -nocase [lindex $b 0] $chan]} { lappend z ${b} }
  }
  close $a
  set n [open $file w]
  foreach k $z {
   if {$k != ""} { puts $n $k }
  }
  close $n
  putlog "Deleted $chan from file." 
 } {
  putlog "$chan does not exist in file."
 }
}
D
Dizzle
Op
Posts: 109
Joined: Thu Apr 28, 2005 11:21 am
Contact:

Post by Dizzle »

thx Sir_Fz, for the code.

You know a good site where i can learn these things, because im dieing too learn TCL in a advanced way ??
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

you better buy a book
D
Dizzle
Op
Posts: 109
Joined: Thu Apr 28, 2005 11:21 am
Contact:

Post by Dizzle »

that could be a good idea, i will check if bol.com has some good books about TCL scripting
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

on a side note, if you are not severely restricted with memory usage (almost never the case with scripts), you better slurp all data from your data file at once, do all data manipulation in memory (that would typically be list & array manipulation), and periodically save all data to the file - as opposed to manipulating data inside files, which of course is slower & more complex
D
Dizzle
Op
Posts: 109
Joined: Thu Apr 28, 2005 11:21 am
Contact:

Post by Dizzle »

Well, it are only channel names of requested channels, dont know if this will take up much memory.

Butt you mean i should use listing insted of file readind and save this too a file i a part of a day like every 45 min ?
D
Dizzle
Op
Posts: 109
Joined: Thu Apr 28, 2005 11:21 am
Contact:

Post by Dizzle »

Well having this problem now

Code: Select all


set file "home/warseeker/channels/chanfile"

set setting(minusers) "15"

set setting(privechan) "#warseeker.prive"

bind pub m|m !gc global:check


proc global:check {nick uhost hand chan text} {
foreach chan [channels] {
check:forward $chan 
}
}

proc check:forward {chan} { 
global file setting
if {[llength [chanlist $chan] < $setting(minusers)]} {
set exists 0 
set chan [lindex [split $arg] 0] 
foreach temp [split [string tolower [read [set inf [open $file]]]] "\n"][close $inf] { 
if {[string equal -nocase [lindex [split $temp] 0] $chan]} { 
set exists 1 
break 
} 
} 
if {$exists} { 
set z "" 
set a [open $file r] 
while {![eof $a]} { 
set b [gets $a] 
if {![string equal -nocase [lindex $b 0] $chan]} { lappend z ${b} } 
} 
close $a 
set n [open $file w] 
foreach k $z { 
if {$k != ""} { puts $n $k } 
} 
close $n 
putlog "Deleted $chan from file." 
puthelp "MSG $setting(privechan) : $chan was deleted, the channel was not having enough users"
utimer 5 [list remove channel $chan]
utimer 7 [list savechannels]
} { 
putlog "ERROR $chan not found in chanlist" 
} 
}
}

Error : Tcl error [global:check]: wrong # args: should be "llength list"

When i rewite the script and make it

Code: Select all

{[llength list [[chanlist $chan] < $setting(minusers)]]} {
it returns all the names off the users on that channel with the msg
Tcl error [global:check]: invalid command name, (and all the names)

So i guess this is not right, i tried a little more things butt then the code i complety messed up,

Can you guys give me some help ?
D
Dizzle
Op
Posts: 109
Joined: Thu Apr 28, 2005 11:21 am
Contact:

Post by Dizzle »

I know the peice of code Sir_Fz had made. is not the problem maker, the peice of code that count the users of a channel is not right, butt i can figger out what cause the usercount is being used by me in another script and it is not given any troubles
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

It's because you've made it all wrong, what are you using brackets for?

Code: Select all

if {[llength [chanlist $chan] < $setting(minusers) ->>> ] <<<-} { 
That's one of the useless brackets not supposed to be in there

Code: Select all

{[llength list [[chanlist $chan] < $setting(minusers)]]} {
Now you've done it again and added more useless brackets not to mention list?!
Not to mention do it all wrong.

You should go through the tcl-commands.doc BEFORE you ask us to fix your code.




PS:

Code: Select all

if {[llength [chanlist $chan]] < $setting(minusers)} {
D
Dizzle
Op
Posts: 109
Joined: Thu Apr 28, 2005 11:21 am
Contact:

Post by Dizzle »

MeTroiD wrote:.

You should go through the tcl-commands.doc BEFORE you ask us to fix your code.
Well, i cant remember that in TCL-documents the way too place brackets is getting explained. :oops:

Well i shall see if i cant find a good guide or manual about placing brackets,

sry for the question :roll:
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

I wasn't talking about the brackets i was talking about what you used for llength.

When you get the errormessage

should be llength list it means use [llength <LIST>]
Not [llength list <LIST>].
Locked