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.

Writing to a file

Old posts that have not been replied to for several years.
Locked
D
Darkj
Halfop
Posts: 86
Joined: Sun Jul 06, 2003 9:58 pm

Writing to a file

Post by Darkj »

I've been working on this for hours and its gone through many revisions.
Basically what I am trying to do, is setup a command like this:

!location BotName BotsLocation(Ingame) What-the-bot-is-doing

Now this is all manual, so I'm just storing it to a file.

Now heres the code:

Code: Select all

proc locate { nick user hand chan arg } {
  set locatebot [lindex $arg 0]
  set location [lindex $arg 1]
  set action [lrange $arg 2 end]
  if {$locatebot == ""} {
    puthelp "PRIVMSG $chan :The bot you specified does not match any bot in my database."
    return 0
  }
  if {$location == ""} {
    puthelp "PRIVMSG $chan :Not a valid location."
    return 0
  }
  if {$action == ""} {
    puthelp "PRIVMSG $chan :You have to specify what the bot is currently doing."
    return 0
  }
  set file [open bots.db w+]
  set found 0
  set index ""
  while {![eof $file]} { 
    gets $file line 
    if {[string match $locatebot [lindex $line 0]]} { 
        set found 1 
    } 
  }
  if {$found == 1} {
  	puts $file "$locatebot $location $action"
  	putserv "PRIVMSG $chan :The location and action of $locatebot have been added to the database."
  	return
  } else {
  	puts $file "$locatebot $location $action"
  	putserv "PRIVMSG $chan :The location and action of $locatebot have been changed in the database."
  	return
  }
} 
Basically i'm trying to check to see if there is an entry for the bot already, and if there is, I want it to replace it, otherwise I want it to add a new entry. If someone can give me some insight as to what is wrong, it would be a big help.

Thanks
User avatar
slennox
Owner
Posts: 593
Joined: Sat Sep 22, 2001 8:00 pm
Contact:

Post by slennox »

Do you have experience with using list commands (lindex, lreplace, etc.)? If so, it would probably be easier for you to read the file into a list variable (where each element represents a line of the file), edit the list, then overwrite the file with the new lines. I find this easier than working with the file directly (although it's probably less efficient).

Code: Select all

set file [open bots.db r]
set lines [split [read -nonewline $file] \n]
close $file
Now you just do your modifications to $lines. When you're done:

Code: Select all

set file [open bots.db w]
puts -nonewline $file [join $lines \n]
close $file
That will overwrite the old file.
D
Darkj
Halfop
Posts: 86
Joined: Sun Jul 06, 2003 9:58 pm

Post by Darkj »

I don't really have much experiencing with lists and working with lists. I understand what you are saying, but I still get this part:

1) Retrieve all items from list (file)
2) Check list to see if item exists and if not add it
3) Create a new list (oldlist + newentry/modified entry)


I understand the saving and reading of the file, just not sure how to parse it to check for things....
User avatar
slennox
Owner
Posts: 593
Joined: Sat Sep 22, 2001 8:00 pm
Contact:

Post by slennox »

The in-between part might look something like

Code: Select all

if {[set n [lsearch -glob $list {$locatebot *}]]} {
  lreplace $list $n $n [list $locatebot $location $action]
} else {
  lappend list [list $locatebot $location $action]
}
I also want to point out at the start of your proc that you should split the $arg before using lindex on it, e.g.

Code: Select all

  set locatebot [lindex [split $arg] 0]
  set location [lindex [split $arg] 1]
  set action [join [lrange [split $arg] 2 end]]
otherwise it may have problems handing input containing special characters like brackets.
D
Darkj
Halfop
Posts: 86
Joined: Sun Jul 06, 2003 9:58 pm

Post by Darkj »

Awesome, got it working now, thanks for the help.
User avatar
slennox
Owner
Posts: 593
Joined: Sat Sep 22, 2001 8:00 pm
Contact:

Post by slennox »

I don't know if you already spotted the error yourself, but the first line should have been:

Code: Select all

if {[set n [lsearch -glob $list {$locatebot *}]] != -1} {
D
Darkj
Halfop
Posts: 86
Joined: Sun Jul 06, 2003 9:58 pm

Post by Darkj »

Yea I caught that
Locked