| View previous topic :: View next topic |
| Author |
Message |
QQleQ Voice
Joined: 20 Nov 2006 Posts: 14
|
Posted: Mon Nov 20, 2006 10:09 pm Post subject: White lines in textfile after write |
|
|
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: |
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
}
|
|
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Mon Nov 20, 2006 11:32 pm Post subject: |
|
|
| Code: |
foreach bn $::deauds {
puts $a [join $bn \n]
}
|
Should do it..
I'd also put in a
| Code: |
if {$bn != ""} {
puts $a [join $bn \n]
}
|
so its not saving blank lines.. |
|
| Back to top |
|
 |
QQleQ Voice
Joined: 20 Nov 2006 Posts: 14
|
Posted: Tue Nov 21, 2006 12:27 am Post subject: |
|
|
Hmz, for some reason that aint working
the text file now looks like this:
| Code: |
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: |
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? |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Tue Nov 21, 2006 3:02 pm Post subject: |
|
|
| You split the line twice, once again with the foreach.. |
|
| Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Thu Nov 23, 2006 3:41 pm Post subject: |
|
|
wouldnt just
| Code: | 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... |
|
| Back to top |
|
 |
|