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.

getting a certain string from a file

Old posts that have not been replied to for several years.
Locked
E
Errtu

Post by Errtu »

Ok, let's say i have a filename that consists of nicknames and hostnames like this:

nick host
nick host
etc.

How can i get a specific line number from the file, and the matching nick. For example, get the first word (=nick) on line 3.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

It depends how you wish to obtain the details.

Do you wish to get the line number by looking up a given nickname?

or get a nickname aby giving a line number?
E
Errtu

Post by Errtu »

i want to get the nickname by providing a line number.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Code: Select all

proc getnick {file line} {
  if {[catch {open "${file}" r} fp]} {
    return -1
  }
  set _T ""
  while {![eof $fp]} {
    lappend _T [gets $fp]
  }
  close $fp
  if {[llength $_T] < $line} {
    return -1
  }
  return [lindex [split [lindex $_T [expr $line - 1]]] 0]
}
Locked