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.

index marker in a file

Old posts that have not been replied to for several years.
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

user wrote:
bind pubm -|- "% !p*" que:poem
what is the "%" and the space for?
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Dedan wrote:what is the "%" and the space for?
% matches 0 or more non-space characters (can be used to match a single word) ...and in a pubm bind the mask is matched against the channel name followed by the text, so the % is used to match any channel name. (you could have used *, but then you'd risk matching things that doesn't start with the trigger)

(all this info can be found in doc/tcl-commands.doc)
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

ok, forget the index suggestion and please answer this:

I want to retrive a certain line from a file and place it in a var.

only 1 line

In the script below i want to do 2 things:
1) test if there is a line 75
2) if so, then retrieve and place it in a var

Code: Select all

  set line_wanted 75
  if {[file exists $my_file]} {
    set opened_file [open $my_file r]

    need to check if line exsist

    need to retrive line

    set found_line   ??????

thanks
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

proc getLineIfItExists {file {line 0}} {
	set lines [split [read [set file [open $file]]] \n]
	close $file
	if {[llength $lines]>$line} {
		lindex $lines $line
	} else {
		error "No such line: $line"
	}
}
EDIT: there was an error in the first proc i pasted...it's been corrected now
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

both my shell accounts are down,
I have no way to test this.

do you think this might work?

Code: Select all

  set line_wanted 75
  if {[file exists $my_file]} {
    set opened_file [open $my_file r]
    if {[info exists [seek opened_file 75 start]]} {
      set found_line [seek opened_file 75 start]
    }
  }

I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Dedan wrote:do you think this might work?
'info exists' is for checking for the existance of variables. 'seek' is for changing the position in a open file and works with bytes, not lines, so unless your file contains only newlines this doesn't make any sense.

The only way to make it work using seek is by having fixed line lengths.


Tip: Install tclsh (tcl shell) on your current computer to test suff in.
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

I am overwhelmed by the commands of tcl to deal with files,
are you saying that there is no way to read a certain line
without knowing its lenght?

In your last proc you showed no way to retrevie the line
so it could be set.
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Dedan wrote:I am overwhelmed by the commands of tcl to deal with files, are you saying that there is no way to read a certain line
without knowing its lenght?
I can see why you're intimidated by this as you're used to mirc's file commands. Tcl deals with files like real programming languages; you get full control at the cost of a bit more coding work. There are no lines in a file, only bytes. :^)
Dedan wrote:In your last proc you showed no way to retrevie the line
so it could be set.
Let me repeat with comments :)

Code: Select all

proc getLineIfItExists {file {line 0}} { 
   # this first line does alot of stuff...
   # first the file is opened for reading (the default mode, so the "r" is not needed
   # then the file pointer returned is stored in the variable "file"
   # set returns the value further up to 'read' which reads the entire file
   # and returns the contents to 'split' which chops it up by the newlines (\n)
   # the resulting list of lines is then stored in 'lines'
   set lines [split [read [set file [open $file]]] \n] 
   # then the file is closed (as we don't need to read from it no more)
   close $file 
   # and THEN we check if the number of lines is > our desired line #
   if {[llength $lines]>$line} { 
      # if it is, we retrieve the line we want using lindex (this value is then returned by the proc...you could of course store it in a variable or what ever you might desire
      lindex $lines $line 
   } else { 
     # if the requested line number is too high, generate an error.
      error "No such line: $line" 
   } 
}


Did that make things clear? :)
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

ok, so the command is:

{getLineIfItExists $opened_file $line_wanted}

??????
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Dedan wrote:ok, so the command is:

{getLineIfItExists $opened_file $line_wanted}

??????
Yup..lose the braces though :) (and feel free to rename the proc ;P)
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

this file has about 2500 lines, is there anyway
to get aroung reading all those lines?
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Dedan wrote:this file has about 2500 lines, is there anyway
to get aroung reading all those lines?
If the contents is fairly static you could compile some sort of index...eg. a list of line lengths (this way you could skip directly to the desired line using 'seek')

Here's a proc reading the file line by line rather than reading it all and splitting it (sort of like mirc does it internally i'd imagine):

Code: Select all

proc getLine {file {line 1}} {
	set f [open $file]
	set i 0
	while {[incr i]<$line} {gets $f}
	gets $f line
	close $f
	set line
}
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

like i said before, my shells are down
i have no way of testing this.

do you think this would work?

Code: Select all

  set line ""
  set line_wanted 75
  if {[file exists $my_file]} {
    set opened_file [open $my_file r]
    set line_number 0
    while {![eof $opened_file]} {
      incr line_number 1
      if {$line_number == $line_wanted} {
        set line [gets $opened_file]
        break
      }
    }
    if {$line == ""} {
      *** error message ***
    }

I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Dedan wrote:like i said before, my shells are down
i have no way of testing this.
like i said before, get tclsh running on your local machine :)
Dedan wrote:do you think this would work?
...
This is how my previous proc works...'gets' reads every byte from the current position in the file to the next newline (or the end of the file, whichever comes first). So each call to 'gets' will cause the access position in the file channel to be moved one line further towards the end of the file.

You just increase the counter but do nothing to the access position in the file channel, so your code will always give you the first line from the file. (because you only 'gets' once)
Locked