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.

seek and catch {close $variable} meaning

Old posts that have not been replied to for several years.
Locked
u
up^to^you

seek and catch {close $variable} meaning

Post by up^to^you »

I just wandering, what is mean the part of this code, especially seek 0 end command,

seek $a 0 end
seek $c 0 end
seek $e 0 end
seek $g 0 end
catch {close $a}
catch {close $c}
catch {close $e}
catch {close $g}


coz i have problem with too many open files when i load this tcl, so how about if i change a little different at above part code and change them like this:


seek $a 0 end
catch {close $a}
}
seek $c 0 end
catch {close $c}
}
etc...

so...problem too many open files will be avoid. Hmm...i wander, would it be the same or will give a different meaning from the old code??
thx, i just want to know how they work, so maybe i'll understand (at least i try to understand) to fix my current problem about this.
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

it will be the same :)

if I remember correctly you opens all the file for reading then if none of it matches you write to the last file.... correct?

if that was the case maybe you should try opening just one file at the time.... open 1--->close 1 open 2--->close 2 ....etc ;)
this probably means that you'll have to rewrite parts of your code..but maybe it's worth a shot :)
Elen sila lúmenn' omentielvo
u
up^to^you

Post by up^to^you »

hmm...you mean like this: ??

if {[string match -nocase "*$text*" $b]} {
return 0
}
seek $a 0 end
catch {close $a}
}
if {[string match -nocase "*$text*" $d]} {
return 0
}
seek $c 0 end
catch {close $c}
}
etc...
puts $a $text
}

putlog "test.tcl loaded"
u
up^to^you

Post by up^to^you »

but seems it make error like this:

[01:57] can't read "text": no such variable
while executing
"string match -nocase "*$text*" $d"
(file "scripts/test.tcl" line 1)
(file "mybot" line 1)
[01:57] * CONFIG FILE NOT LOADED (NOT FOUND, OR ERROR)

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

Post by ppslim »

You would get that, as the error sugests, whent he variable doesn't exist.

You havn't exactly shown much of the code, other than the file close portion.

At the moment, the script does the following
open FILEA
open FILEB
open FILEC
open FILED

perform some command on the file

close FILEA
close FILEB
close FILEC
close FILED
This means, it opne 4 files at the same time. WHat you need, is to only open once at a time

EG
open FILEA
perform action of file
close FILEA

open FILEB
perform action on file
close FILEB

open FILEC
perform action on file
close FILEC

open FILED
perform action on file
close FILED
[/code]
u
up^to^you

Post by up^to^you »

well..i put them like this:

set a [open $files(1) r+]
set b [read $a]
if {[string match -nocase "*$text*" $b]} {
return 0
}
seek $a 0 end
catch {close $a}
set c [open $files(2) r+]
set d [read $c]
if {[string match -nocase "*$text*" $d]} {
return 0
}
etc...
seek $g 0 end
catch {close $g}
puts $a $text

but when i test add something to my egg, it was killed, no error just seems not enough memory or ram. so...how to make they work well without spend to many memory of my shell?
thx
h
hah`

Post by hah` »

up^to^you wrote:well..i put them like this:

set a [open $files(1) r+]
set b [read $a]
if {[string match -nocase "*$text*" $b]} {
return 0
}
seek $a 0 end
catch {close $a}
Hmm... where's the sense in reading a file, jumping to the end (where you already are when reading the file to the end ;-) ) and then closing the file? ...and you return before closing the file if it matches -> open file descriptor
u
up^to^you

Post by up^to^you »

:-? well....actually i dont have any idea, i already try to make variations of that code, but the result is error and this is my last alternative variation and it seems dont have any error (my bot can be loaded) but if i test and add something to it, it killed (i think it because lack of memory of my shell) and now completely confuse and really need help or suggest from anyone, i dont pretty much smart about tcl, so can you make more specify what should i do? thx
h
hah`

Post by hah` »

seek jumps to the specified file position...
seek $fa 0 end -> jump to 0 bytes after the end of the file... that's where you are, when reading a file from the start to the end.

a 'catch { close $fd }' closes file descriptor $fd and returns no error, even if the file is closed already

This appends $text to the first opened file, if $text is in none of the files

Code: Select all

set fa [open $files(1) r+]
set txt [read $fa]
### here you're at the end of $files(1) ...
if {[string match -nocase "*$text*" $txt]} {
    ### remember to close $file(1) if $text is there:
    catch { close $fa }
    return 0
} 

set fb [open $files(2) r]
set txt [read $fb]
catch { close $fb }
if {[string match -nocase "*$text*" $txt]} {
    ### remember to close $file(1) if $text is there:
    catch { close $fa }
    return 0
}
... snip ....
catch { close $fx }
if {[string match -nocase "*$text*" $txt]} {
    catch { close $fa }
    return 0
}
puts $fa $text
catch { close $fa }
If you want to replace the content of $file(1) insert a 'seek $fa 0 start' before the 'puts $fa $text'
u
up^to^you

Post by up^to^you »

i change my code to your advice like this:

Code: Select all

set a [open $files(1) r+] 
set b [read $a]
if {[string match -nocase "*$text*" $b]} { 
catch {close $a} 
return 0 
}
set c [open $files(2) r+]
set d [read $c]
if {[string match -nocase "*$text*" $d]} { 
catch {close $c}
return 0 
}
...etc....
puts $a $text
catch {close $a}
}
putlog "new.tcl loaded"

but still my eggs getting killed with quit message (Client closed connection), and closed by remote section when i'm using telnet if i test by adding $text that already on the file (i want to test if the code works to denied a text that already on file), but the result is always killed....
Why? :(

[/code]
Locked