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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading in data in columns and splitting it correctly 1

Status
Not open for further replies.

scottjn

Technical User
Feb 15, 2011
6
0
0
US
Hello there! I would sincerely appreciate any advice you folks can offer, I've been trying to figure all this out for a while now with little luck. Though I have experience in C++ and FORTRAN I am pretty new to Tcl and am still trying to wrap my head around some of its features.

I have a series of files, named water_step_xxxxx.wat, where xxxxx is a five digit integer running by 2's from 00000 to 40000. These files each contain data in 2 columns generated by a FORTRAN program I wrote. The first column is in FORTRAN's f6.2 format (124.23, or 2.00 for instance) while the second is in f10.5 format.

I have thus far figured out how to open the files in a while loop, and have cobbled together the following sort of thing (file reading loop omitted for clarity) for reading the data into a variable:

set fp [open "C:\\wat\\water_step_00000.wat" r]
set file_data [read $fp]
close $fp
set data [split $file_data "\n"]

So my only real issue at this point (that I know of!) is how to get those columns of numbers into 2 separate arrays or lists. I know that the "format" statement exists in Tcl, but it doesn't appear to have the same functionality as in FORTRAN.

Can some kind soul share the most straightforward way to read the data from these .wat files into a list or array of numbers that I can then perform simple arithmetic with? Also, what type of variable would data be, in the code example above? I had assumed it would be an array, but none of the array specific commands appear to work with the data variable.

Thanks sincerely for any help you can provide, I'm hoping that perhaps there is some feature of Tcl that I am simply unfamiliar with that might help in this task.
 
Hi

scottjn said:
how to get those columns
Not sure what you want to do there, but sounds like you need [tt]lindex[/tt] :
Code:
[b]set[/b] data [teal][[/teal][b]split[/b] [navy]$file_data[/navy] [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal]][/teal]
[b]puts[/b] [green][i]"second line is [lindex $data 1]"[/i][/green]
( Note that [tt]lindex[/tt] counts from 0. )
scottjn said:
Also, what type of variable would data be, in the code example above?
split said:
split - Split a string into a proper Tcl [highlight]list[/highlight]
scottjn said:
I had assumed it would be an array, but none of the array specific commands appear to work with the data variable.
In Tcl the array is associative array ( or hash in other languages ).
The closest to an indexed array is the list. Less convenient than usual arrays in other languages, because it can be handled through specific commands, like the above used [tt]lindex[/tt].


Feherke.
 
Hi feherke, thanks very much for your help. OK, now I see that data is indeed a list and can be accessed using the suite of list commands. The problem I still have though is that I now have a list "data" with entries being something like:

22.97 -10.03061
36.35 20.01254
35.59 0.00575
31.00 -0.01740
41.64 -0.00647
49.27 -0.00414
39.12 -0.00448
38.76 4.00489
42.43 -0.00042
35.81 3.02147
28.47 -0.01777
17.50 -0.13863
21.28 1.08894

How can I get the columns separated? What I really need is the second column's numbers in a list, with each index corresponding to a different line of the file. I tried doing something similar to what I had before for splitting the file up by lines using "\n". I tried:
Code:
set data [split $file_data " "]
but unfortunately this yields pretty bad results. I end up with lots of empty elements in the list because for some entries in the file the number in the second column has a number in the tens place, but for some other entries it doesn't.

One other question, assuming I get a list of floating point numbers, how does one use list elements in mathematical operations? I've tried the following but nothing seems to work.
Code:
set a [expr lindex $data 0 + 1]
set a [expr {lindex $data 0} + 1]
set a [expr (lindex $data 0) + 1]

What is the syntax for accessing the data contained in the list elements and using that data for math operations?

Thanks again for your help!!
 
Hi

Still not sure what are you doing there, so I just answer the questions.
scottjn said:
How can I get the columns separated?
For example you can create a separate list $col2 containing only column 2 values with this :
Code:
[b]foreach[/b] [teal]{[/teal]one[teal]}[/teal] [navy]$data[/navy] [teal]{[/teal]
  [b]lappend[/b] col2 [teal][[/teal][b]lindex[/b] [navy]$one[/navy] [purple]1[/purple][teal]][/teal]
[teal]}[/teal]
scottjn said:
how does one use list elements in mathematical operations?
Working with the $col2 list created with above code :
Code:
[b]set[/b] a [teal][[/teal][b]expr[/b] [teal][[/teal][b]lindex[/b] [navy]$col2[/navy] [purple]0[/purple][teal]][/teal] [teal]+[/teal] [purple]1[/purple][teal]][/teal]
Or the same using the original $data list :
Code:
[b]set[/b] a [teal][[/teal][b]expr[/b] [teal][[/teal][b]lindex[/b] [teal][[/teal][b]lindex[/b] [navy]$data[/navy] [purple]0[/purple][teal]][/teal] [purple]1[/purple][teal]][/teal] [teal]+[/teal] [purple]1[/purple][teal]][/teal]


Feherke.
 
feherke, thank you!! This was exactly what I needed! It never occurred to me to treat each element of the $data list as a list itself, and then just retrieve the second element of it. This is perfect, thanks so much for your patience and help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top