Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to do data manipulations with tcl?

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
How to do data manipulations with tcl? For instance I want to be able to open a file and select a given column and stores this into a variable.

Basically I am looking some tcl examples of what awk can easily do.

Any help/links would be much appreciated.
 
Getting an element in a matrix file:
Code:
  # set a matrix file
  set fn myfile
  set fp [open $fn w]
  foreach row {0 1 2}   {
    foreach col {0 1 2}     {
      # write and element and its separator
       puts -nonewline $fp "r${row}c${col} " 
    }
    # write the end of line
    puts $fp ""
  }
  close $fp
  # the proc
  proc getxy {file row col}   {
    set fp [open $file]
    set n -1
    while {![eof $fp]}     {
      # read a line of elements 
      set line([incr n]) [gets $fp] 
    }
    close $fp
    # get the line
    set theline $line($row)
    # return the element
    return [lindex $theline $col]
  }
  # get the 3rd element of the 2nd row
  puts [getxy $fn 1 2]
File commands are: open, close, gets, puts
Each record of the file can be considered a list of elements.
Getting the file in memory, one can access each line by gets.
And each element inside a line by lindex.

bonne chance !

ulis
 
In place of:
"Getting the file in memory, one can access each line by gets"
read:
"Each line can be accessed by gets"

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top