Parsing files and lines using split 
Author Message
 Parsing files and lines using split

Quote:

> I am reading in a flat text file where each line contains a number of
> tab delineated fields. The code I am using to split the file into lines
> and then print out certain fields with formatting is as follows.  It
> works fine, but I am wondering if there is a better way to do this.

>     set file_ptr [open entries "r"]

>     foreach line [split [read -nonewline $file_ptr] \n] {
>         set items [split $line \t]
>         puts [format "%-12s %-10s%10.2f   %s\n" [lindex $items 0] \
>              [lindex $items 1] [lindex $items 2] [lindex $items 4]]
>     }

> Any suggestions would be appreciated. Thanks.

If there are a fixed number of columns per line, then create one
variable
per column and do something like the following:

set file_ptr [open entries "r"]
foreach {a b c d e} [read $file_ptr [file size entries]] {
    puts [format "%-12s %-10s%10.2f   %s\n" $a $b $c $e]

Quote:
}

close $file_ptr

I believe this is probably (nearly) the best solution.
It is a little more memory hungry (depending on the size
of the file) than one of the previous solutions, because
this reads the whole file into memory, rather than reading
it line by line.  If the line is going to be very big (especially
if it is bigger than the amount of physical memory) then
you will want to use a solution that reads the file line
by line, rather than all at once.

The version of tcl that you are using might make a difference
as well (as to what kind of performance you will get).

--Dan



Sat, 01 Feb 2003 03:00:00 GMT  
 Parsing files and lines using split
I am reading in a flat text file where each line contains a number of
tab delineated fields. The code I am using to split the file into lines
and then print out certain fields with formatting is as follows.  It
works fine, but I am wondering if there is a better way to do this.

    set file_ptr [open entries "r"]

    foreach line [split [read -nonewline $file_ptr] \n] {
        set items [split $line \t]
        puts [format "%-12s %-10s%10.2f   %s\n" [lindex $items 0] \
             [lindex $items 1] [lindex $items 2] [lindex $items 4]]
    }

Any suggestions would be appreciated. Thanks.

Richard Burton



Sun, 02 Feb 2003 12:37:37 GMT  
 Parsing files and lines using split

Quote:

> I am reading in a flat text file where each line contains a number of
> tab delineated fields. The code I am using to split the file into lines
> and then print out certain fields with formatting is as follows.  It
> works fine, but I am wondering if there is a better way to do this.

How about:

    set file_ptr [open entries "r"]

    while {[gets $file_ptr line] != -1} {
        set items [split $line \t]
        puts [format "%-12s %-10s%10.2f   %s\n" [lindex $items 0] \
             [lindex $items 1] [lindex $items 2] [lindex $items 4]]
    }

Regards,
--
Jan Nijtmans, CMG Oost-Nederland B.V.


url:   http://purl.oclc.org/net/nijtmans/



Sun, 02 Feb 2003 12:58:18 GMT  
 Parsing files and lines using split

Quote:

>     set file_ptr [open entries "r"]

>     foreach line [split [read -nonewline $file_ptr] \n] {
>         set items [split $line \t]
>         puts [format "%-12s %-10s%10.2f   %s\n" [lindex $items 0] \
>              [lindex $items 1] [lindex $items 2] [lindex $items 4]]
>     }

> Any suggestions would be appreciated. Thanks.

If you're going to read the whole file into memory, then you can improve
performance by telling [read] how much data to read in.  It may get much
faster if you change the first foreach line to:

 foreach line [split [read -nonewline $file_ptr [file size entries] \n]
{

This, and other performance improving tips, can be found on the Tcler's
Wiki at http://purl.org/thecliff/tcl/wiki/TclPerformance

Bob
--

Mayo Foundation                                           (507) 284-2702
Rochester MN, 55905 USA   http://www.mayo.edu/sppdg/sppdg_home_page.html



Sun, 02 Feb 2003 03:00:00 GMT  
 Parsing files and lines using split
Quote:
> Thanks for all the suggestions. I was just what I was looking for.

> Richard Burton




Sun, 02 Feb 2003 03:00:00 GMT  
 Parsing files and lines using split
Quote:


> > I am reading in a flat text file where each line contains a number of
> > tab delineated fields. The code I am using to split the file into lines

    ^^^^^^^^^^^^^^

Quote:
> If there are a fixed number of columns per line, then create one
> variable
> per column and do something like the following:

> set file_ptr [open entries "r"]
> foreach {a b c d e} [read $file_ptr [file size entries]] {
>     puts [format "%-12s %-10s%10.2f   %s\n" $a $b $c $e]
> }
> close $file_ptr

But this is only possible, if the fields don't contain any whitespace
characters

Uwe

--

http://rcswww.urz.tu-dresden.de/~koloska/
--                                    --
right now the web page is in german only
but this will change as time goes by ;-)



Mon, 03 Feb 2003 07:59:21 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. text parsing by splitting a line

2. Need to split file with two reports per line

3. one liner? --- split file at empty line

4. Splitting lines in a file

5. how to split each new line into a list, considering quoted new line characters

6. How to parse command-line using TCL ?

7. PARSE, WORD and end of line in files

8. PARSE, WORD and end of line in files

9. GetPot 1.0: input file and command line parsing.

10. Input File & Command Line Parsing

11. parsing a line in a file

12. Parsing multi-line fixed-structure text file help?

 

 
Powered by phpBB® Forum Software