egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

storing multiple variables (counters + names) in 1 file

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
BCyo+8C4
Voice


Joined: 03 Sep 2006
Posts: 24

PostPosted: Wed Feb 07, 2007 6:01 am    Post subject: storing multiple variables (counters + names) in 1 file Reply with quote

Hi,
is it possible to store counters for multiple persons in one file?
I know how to do it for one file per person, but having just one file would be somewhat nicer. I imagine the file looking like this:

nick 123
anothernick 456
...

Would be nice if someone could help me
Back to top
View user's profile Send private message
BCyo+8C4
Voice


Joined: 03 Sep 2006
Posts: 24

PostPosted: Wed Feb 07, 2007 6:31 am    Post subject: Reply with quote

just had the idea of writing and deleting the lines like this, but i'd still need a way to read a) certain entries (to get the old value) and b) all values (to print stats like "nick: 123, anothernick: 456" to channel. if someone could please give me a hint on how to do that.. Smile


exec echo -e "$nick: $number" >> counter.txt

exec awk -f alfa.awk -v was=$nick counter.txt
Back to top
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Wed Feb 07, 2007 11:51 pm    Post subject: Reply with quote

as it's been pointed out countless times already, you should read the entire file, once only, on script's startup, into a list; then manipulate that list as you wish, typically on events that your script handles; save the list periodically into a file
_________________
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code
Back to top
View user's profile Send private message Visit poster's website
BCyo+8C4
Voice


Joined: 03 Sep 2006
Posts: 24

PostPosted: Thu Feb 08, 2007 5:55 am    Post subject: Reply with quote

I used search but couldn't find anything. Could you please point me to a thread with instructions/examples?
Back to top
View user's profile Send private message
rosc2112
Revered One


Joined: 19 Feb 2006
Posts: 1454
Location: Northeast Pennsylvania

PostPosted: Thu Feb 08, 2007 6:11 am    Post subject: Reply with quote

Plenty of examples of reading/writing files, there's even a FAQ about it under "basic file operations" http://forum.egghelp.org/viewtopic.php?t=6885

.. Also look at the lindex command.
Back to top
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Thu Feb 08, 2007 6:29 am    Post subject: Reply with quote

BCyo+8C4 wrote:
I used search but couldn't find anything. Could you please point me to a thread with instructions/examples?


http://forum.egghelp.org/viewtopic.php?t=11184
_________________
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code
Back to top
View user's profile Send private message Visit poster's website
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Thu Feb 08, 2007 7:10 am    Post subject: Reply with quote

I think an array is well suited for the type of data you want to store...
Code:
# this proc will save an array as tcl code that will create the array when executed
proc savearray {array file {mode w}} {
   upvar 1 $array var
   set f [open $file $mode]
   # the file will be empty if no array by that name exists.
   if {[array exists var]} {
      puts $f [list array set $array [array get var]]
   }
   close $f
}

# create some elements
set counter(nick) 123
set counter(anothernick) 456

# save them:
savearray counter counterdata.tcl

# load them back in:
source counterdata.tcl

The concept is identical to the code demond provided, except the array is stored as tcl code instead of storing list elements as lines in the file.
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
BCyo+8C4
Voice


Joined: 03 Sep 2006
Posts: 24

PostPosted: Fri Feb 09, 2007 7:25 am    Post subject: Reply with quote

i tried you tcl array solution, but i don't really get it

counterdata.tcl:
Code:
array set counter {nick 123 anothernick 456}


the normal tcl:
Code:
bind msg n !trigger      increasecounter

proc increasecounter {nick uhost hand text} {
   source counterdata.tcl
   
   foreach counter nickn {
           set counter($nickn)
   }
   
   savearray counter counterdata.tcl
   # debug line
   putserv "PRIVMSG $nick :$counter(nick)"
}

proc savearray {array file {mode w}} {
   upvar 1 $array var
   set f [open $file $mode]
   # the file will be empty if no array by that name exists.
   if {[array exists var]} {
      puts $f [list array set $array [array get var]]
   }
   close $f
}


I just don't get how/where to put the array data in normal strings and increase one of them. Would be nice if you could point out what I have to do.
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Fri Feb 09, 2007 8:43 am    Post subject: Reply with quote

BCyo+8C4 wrote:
I just don't get how/where to put the array data in normal strings and increase one of them. Would be nice if you could point out what I have to do.

There is (like demond said) no need to read/parse the file every time you want to access the data. Just load it ONCE on startup and keep the data in a global variable. Then, on a regular interval OR when you make changes (if you need the file to match the actual data at all times) save it.

Eg:
Code:
# load the data if it exists
if {[file exists counterdata.tcl]} {
   source counterdata.tcl
}
# saving it when ever the userfile/channel file is saved:
bind evnt - save saveCounters
proc saveCounters x {
   global counter
   savearray counter counterdata.tcl
}
# /msg bot !incall will increase ALL counters by 3
bind msg - !incall incallcounters
proc incallcounters {nick uhost hand text} {
   global counter
   foreach cnick [array names counter] {
      incr counter($cnick) 3
   }
}
# /msg bot !incme will increase YOUR counter by 1 and display the new value
bind msg - !incme incyourcounter
proc incyourcounter {nick uhost hand text} {
   global counter
   # first, make sure there is a counter element for that nick...
   if {[info exists counter($nick)]} {
      incr counter($nick)
   } else {
      # if it doesn't exist, create it...
      set counter($nick) 1
   }
   puthelp "PRIVMSG $nick :Your new count is $counter($nick)"
}

_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
BCyo+8C4
Voice


Joined: 03 Sep 2006
Posts: 24

PostPosted: Fri Feb 09, 2007 1:52 pm    Post subject: Reply with quote

thank you very much, i made it work Smile wouldn't have been able to do so without your help
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Fri Feb 09, 2007 2:49 pm    Post subject: Reply with quote

You should add
Code:
bind evnt - prerehash saveCounters
to make sure you don't load old data if you .rehash ...or you could prevent it from loading the file if the counter array exists...
Code:
if {[file exists counterdata.tcl]&&![info exists counter]} {
   source counterdata.tcl
}

_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber