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.

White lines in textfile after write

Help for those learning Tcl or writing their own scripts.
Post Reply
Q
QQleQ
Voice
Posts: 14
Joined: Mon Nov 20, 2006 10:05 pm

White lines in textfile after write

Post by QQleQ »

Hi there

All this script does is writing sentences or words to a textfile.. if its not in either deaud.txt or god.txt already.

Its working like a charm, however, i cant seem to get rid of some strange white lines (line breaks) in the text file. They keep comming back, even though i edit them out using vi.

Help apreciated.

Code: Select all

bind pub n !deaud add:deaud

set deauds [split [read [set file [open "deaud.txt"]]] \n][close $file]
unset file
set god [split [read [set file [open "god.txt"]]] \n][close $file]
unset file

proc add:deaud {n u h c a} {

 global deauds
 global god

 if { [set b [split $a]] != "" } {
 if {[lsearch -exact $deauds [set b [split $a]]] == -1} {
 if {[lsearch -exact $god [set b [split $a]]] == -1} {
  lappend deauds $b
  putmsg $c "$b toegevoegd aan de deaudenlijst."
  save:deaud
 } {
  putmsg $c "$b staat al op de godenlijst."
 }
 } {
  putmsg $c "$b staat al op de deaudenlijst."
 }
 } {
    set qot_fd [open "deaud.txt" r]
    for {set qot_cnt 0} { ![eof $qot_fd] } { incr qot_cnt } {
        gets $qot_fd qot_list($qot_cnt)
    }
    close $qot_fd
    set qot_cnt [expr $qot_cnt - 2]
    set qot_sel $qot_list([set qot_cur [rand [expr $qot_cnt + 1]]])
    putmsg $c "$qot_sel moet deaud. ([expr $qot_cur + 1] van [expr $qot_cnt + 1])"
 }
}

proc save:deaud {} {
 set a [open "deaud.txt" w]
 foreach bn $::deauds {
  puts $a $bn
 }
 close $a
}
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Code: Select all

foreach bn $::deauds {
  puts $a [join $bn \n]
 } 
Should do it..

I'd also put in a

Code: Select all

if {$bn != ""} { 
   puts $a [join $bn \n]
}
so its not saving blank lines..
Q
QQleQ
Voice
Posts: 14
Joined: Mon Nov 20, 2006 10:05 pm

Post by QQleQ »

Hmz, for some reason that aint working

the text file now looks like this:

Code: Select all

Zinloos Geweld
deadscripts
werkoverleg
maczut
loppie

Newlines

Het oude god-script

TiefusLineBreaks
Where the newly added lines are added from loppie (with this script) (the older lines where manually added with vi)

The script now looks like this:

Code: Select all

bind pub - !deaud add:deaud

set deauds [split [read [set file [open "deaud.txt"]]] \n][close $file]
unset file
set god [split [read [set file [open "god.txt"]]] \n][close $file]
unset file

proc add:deaud {n u h c a} {

 global deauds
 global god

 if { [set b [split $a]] != "" } {
 if {[lsearch -exact $deauds [set b [split $a]]] == -1} {
 if {[lsearch -exact $god [set b [split $a]]] == -1} {
  lappend deauds $b
  putmsg $c "$b toegevoegd aan de deaudenlijst."
  save:deaud
 } {
  putmsg $c "$b staat al op de godenlijst."
 }
 } {
  putmsg $c "$b staat al op de deaudenlijst."
 }
 } {
    set qot_fd [open "deaud.txt" r]
    for {set qot_cnt 0} { ![eof $qot_fd] } { incr qot_cnt } {
        gets $qot_fd qot_list($qot_cnt)
    }
    close $qot_fd
    set qot_cnt [expr $qot_cnt - 2]
    set qot_sel $qot_list([set qot_cur [rand [expr $qot_cnt + 1]]])
    putmsg $c "$qot_sel moet deaud. ([expr $qot_cur + 1] van [expr $qot_cnt + 1])"
 }
}

proc save:deaud {} {

set a [open "deaud.txt" w]

foreach line [split $::deauds \n] {
   if {$line != "" && $line != "\n"} {
    puts $a [join $line \n]
   }
}


 close $a
}
What am i doing wrong?
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

You split the line twice, once again with the foreach..
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

wouldnt just

Code: Select all

proc save:deaud {} {
   set a [open "deaud.txt" w]
      puts -nonewline $a [join $::deauds \n]
   close $a
}
do the trick?

PS: I would move "[close $file]" to the next line and remove [] braces. close is not suposed to return anything but an empty string, but I still wouldn't add a probably empty string to something I want to parse as a consident list.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Post Reply