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 

Read/Write specific line

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    egghelp.org community Forum Index -> Archive
View previous topic :: View next topic  
Author Message
Ofloo
Owner


Joined: 13 May 2003
Posts: 953
Location: Belguim

PostPosted: Fri Sep 26, 2003 5:51 pm    Post subject: Read/Write specific line Reply with quote

How do i read a specific line from txt and or change it like i read line 5 and or i want to write to line 5 .. the reading i can do with counter and while but writing how would i do that ?
_________________
XplaiN but think of me as stupid
Back to top
View user's profile Send private message Visit poster's website
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Fri Sep 26, 2003 9:32 pm    Post subject: Reply with quote

Code:
# Fetch a single line
proc getLine {file line} {lindex [split [read [set f [open $file]]][close $f] \n] $line}

# Insert data at the given line
# Accepts the same values for line number as 'linsert' does for index (check your manual)
# Will create the file if it doesn't exist.
proc addLine {file line data} {
   if {[file readable $file]} {
      set d [split [read [set f [open $file r+]]] \n]
      seek $f 0
   } {
      set d ""
      set f [open $file w]
   }
   puts -nonewline $f [join [linsert $d $line $data] \n]
   close $f
}

# Replace one or more lines
# Usage: replaceLines <file> <first line> <last line> [replacement data (optional)]
# Accepts the same values for indexes as 'lreplace' (check your manual)
proc replaceLines {args} {
   if {[llength $args]>=3} {
      foreach {file start end} $args break
      set cmd [list lreplace [split [read [set f [open $file r]]] \n] $start $end]
      close $f
      if {[llength $args]>3} {lappend cmd [lindex $args 3]}
      puts -nonewline [set f [open $file w]] [join [eval $cmd] \n]
      close $f
   } {
      error "wrong # args: should be \"[lindex [info level 0] 0] file start end ?replacement?\""
   }
}

...btw: line numbers are zero-based, like lists, meaning the first line is called "0" Smile
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
stdragon
Owner


Joined: 23 Sep 2001
Posts: 959

PostPosted: Sat Sep 27, 2003 8:05 pm    Post subject: Reply with quote

Quote:

# Fetch a single line
proc getLine {file line} {lindex [split [read [set f [open $file]]][close $f] \n] $line}


Heh why do you obfuscate things so much? There's really nothing wrong with using a few more lines :)
Back to top
View user's profile Send private message Visit poster's website
Ofloo
Owner


Joined: 13 May 2003
Posts: 953
Location: Belguim

PostPosted: Sun Sep 28, 2003 2:17 pm    Post subject: Reply with quote

tnx verry mutch ima try it test it abuse it .. Smile and ill let you know if something is wrong hehe
_________________
XplaiN but think of me as stupid
Back to top
View user's profile Send private message Visit poster's website
Ofloo
Owner


Joined: 13 May 2003
Posts: 953
Location: Belguim

PostPosted: Sun Sep 28, 2003 2:22 pm    Post subject: Reply with quote

might sound stupid, but i am supposed to open the file write before i do this and close after ... and do i require to use it in a while or just like that ...

Quote:
# Fetch a single line
proc getLine {file line} {lindex [split [read [set f [open $file]]][close $f] \n] $line}

_________________
XplaiN but think of me as stupid
Back to top
View user's profile Send private message Visit poster's website
Ofloo
Owner


Joined: 13 May 2003
Posts: 953
Location: Belguim

PostPosted: Wed Oct 08, 2003 4:48 pm    Post subject: Reply with quote

could you give me a write syntax cause ive been reading the manual on lreplace and trying to understand it but i am affraid my knowledge about this topic isn't what it supposed to be to understand the therms that are being used ?

so what is the input syntax for your script ? i always get funny out puts :/
_________________
XplaiN but think of me as stupid
Back to top
View user's profile Send private message Visit poster's website
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Wed Oct 08, 2003 7:28 pm    Post subject: Reply with quote

stdragon wrote:
Quote:
proc getLine {file line} {lindex [split [read [set f [open $file]]][close $f] \n] $line}

Heh why do you obfuscate things so much? There's really nothing wrong with using a few more lines Smile


Actually, in this case the "ubfuscation" was for a reason. If I were to store the contents of the file in a variable, the proc would require 2*[file size $file] to execute (because a copy of the data is passed along to the commands when variable substitution is performed (at least that's how it is in older tcl versions))
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Wed Oct 08, 2003 7:30 pm    Post subject: Reply with quote

Ofloo wrote:
could you give me a write syntax cause ive been reading the manual on lreplace and trying to understand it but i am affraid my knowledge about this topic isn't what it supposed to be to understand the therms that are being used ?

so what is the input syntax for your script ? i always get funny out puts :/

I wrote a line on how to use the last two procs (re-read my post). There might be bugs in my code though (I didn't test it iirc Razz) Give me a example of what you're doing and what you expect as a result and I'll try to help you on the right track Smile
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
Ofloo
Owner


Joined: 13 May 2003
Posts: 953
Location: Belguim

PostPosted: Wed Oct 08, 2003 11:18 pm    Post subject: Reply with quote

i can't be sur because i don't know what the imput is i tryed various combinations

like file start end ..

i get stuff like this is not an integer ..

i know that doens't make the script wrong its me i know that but just not sur what this exactly means

# Usage: replaceLines <file> <first line> <last line> [replacement data (optional)]
_________________
XplaiN but think of me as stupid
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    egghelp.org community Forum Index -> Archive 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