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.

id number per file

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

id number per file

Post by ultralord »

hello i have one proc that:


set g_last2 [linsert $g_last2 end $g_lastinfo]
set fileio [open /home/ultralord/www/g_last2.txt "w"]
puts $fileio $g_last2
flush $fileio
close $fileio


that creates one txt file with name g_last2 .. i want w8 that proc run's second time then i want to create txt file with name g_last201 .. is it possible?

01=id number and after that 02.. etc..


thnx in advance
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I'd suggest you store a counter in some global variable, say file_counter, and use something like this:

Code: Select all

#instantiate the file_counter variable upon load
set file_counter 0

...
proc yourproc ....
 set fileio [open "/home/ultralord/www/g_last2[format "%02u" [incr ::file_counter]]" "w"]
...
Here we use incr to increase the value of the counter upon every iteration of the code, and then format to achieve the desired 2-digit format with padding zeros.
NML_375
User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

Post by ultralord »

thnx :o i found another way yo do my job :P

i make some procs more and i make same expr and done..

thanks that will be very usefull in the future

also. how i can resolve the id when i rehash my bot if i use it?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Resolve? Could you please elaborate that a bit?
NML_375
User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

Post by ultralord »

if i rehash or restart the bot the counter number will be 0 and then will be start count again from begging ..
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Ohh, you meant restore..
Well, one way would be to include code to (re)write a simple tcl-script (loaded from your script once it completes) which basically sets the variable to the previous value...
NML_375
g
game_over
Voice
Posts: 29
Joined: Thu Apr 26, 2007 7:22 am

Post by game_over »

I know another way

Code: Select all

set directory "txts/"
#only txt files :)
set pattern "*.txt"

if {[catch {glob "${directory}${pattern}"} files]} {
	return "No files in this dir"
} else { 
	if {[string match "*[info script]*" $files]} {
		set listOffilesInThistDir [expr [llength $files] -1]
	}
   }
now when you know filecount you script newfile to $listOffilesInThistDir + 1 :)
User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

Post by ultralord »

good try :) also if you can make another proc and you can put one number like 1 1 1 1 and when you rehash the bot the variable read how many times you have the number 1 1 1 1 into a txt file and then you have your latest id number . :idea:
Post Reply