For Programmers: Free Programming Magazines  


Home > Archive > Tcl > June 2005 > cat the file and put it in variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author cat the file and put it in variables
Nitin

2005-06-06, 3:58 am

Hi
I am new to Tcl. I need to do very simple stuff. Cat a file that has
100 lines and every line has two fields. I need to cat the file one
line at a time, and take the value of the fields in two varaibles. Do
some processing and then when file hits EOF quit.


Any comments will be appreciated.

Thanks
Nitin

abhishekkabra@gmail.com

2005-06-06, 3:58 am

to simple
1.open the file set file [ open filename r ]
2.read the whole file set str [read $file]
3.split str with "\n" or "\r" newline character
4.foeach loop will help you to access whole line ( that has 2 elements
)

Abhishek

SM Ryan

2005-06-06, 8:57 am

abhishekkabra@gmail.com wrote:
# to simple
# 1.open the file set file [ open filename r ]

1.5. fconfigure $file -translation (whatever)

# 2.read the whole file set str [read $file]
# 3.split str with "\n" or "\r" newline character
# 4.foeach loop will help you to access whole line ( that has 2 elements

The translation option can be used to convert \r, \n, or \r\n into \n during
all input and output operations.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
Donal K. Fellows

2005-06-06, 8:57 am

Nitin wrote:
> I am new to Tcl. I need to do very simple stuff. Cat a file that has
> 100 lines and every line has two fields. I need to cat the file one
> line at a time, and take the value of the fields in two varaibles. Do
> some processing and then when file hits EOF quit.


This feels a little bit like homework, so I won't give a full solution.
I'll just point to the pieces you need.

To read the file in, you'll want [open], [read] and [close]. Then you'll
want to [split] the data by newlines (100 lines is a small file) and
iterate over the list of lines using [foreach]. You will probably want
to parse each line with [split] or [regexp] (choosing which to use
depends on the detailed format of the data being read).

Alternatively, you could use [open], [while], [eof], [gets], parse the
line that you've read (see notes above) and put [close] after the loop.

You might or might not find helpful examples by looking on our wiki,
which is nicely indexed by google BTW. ;-)

Donal.
Khaled

2005-06-06, 8:57 am



Donal K. Fellows wrote:
>
> Alternatively, you could use [open], [while], [eof], [gets], parse the
> line that you've read (see notes above) and put [close] after the loop.


This is the fastest option of the two :^)

Khaled

Cameron Laird

2005-06-06, 4:00 pm

In article <1118049979.883757.307600@o13g2000cwo.googlegroups.com>,
Khaled <nospam.ksubs@free.fr> wrote:
>
>
>Donal K. Fellows wrote:
>
>This is the fastest option of the two :^)
>
>Khaled
>


Not always. It depends on specifics of the execution environment
and data.
Cameron Laird

2005-06-06, 4:00 pm

In article <11a7su22bqpjheb@corp.supernews.com>,
SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
>abhishekkabra@gmail.com wrote:
># to simple
># 1.open the file set file [ open filename r ]
>
>1.5. fconfigure $file -translation (whatever)
>
># 2.read the whole file set str [read $file]
># 3.split str with "\n" or "\r" newline character
># 4.foeach loop will help you to access whole line ( that has 2 elements
>
>The translation option can be used to convert \r, \n, or \r\n into \n during
>all input and output operations.

Michael A. Cleverly

2005-06-10, 4:02 pm

On Mon, 6 Jun 2005, Cameron Laird wrote:

> Indeed it can--but to what purpose that bears on the original requirements?
>
> I'm tempted to submit
>
> # *This* certainly is fragile in the face of exceptions.
> foreach {first second} [split [exec cat $filename] \n] {
> puts "The two items are '$first' and '$second'."
> }
>


My first reaction would be:

set fp [open $filename]
while {[gets $fp line] != -1} {
foreach {first second} $line break
# do whatever
}
close $fp

or, as a pure-Tcl alternative to [exec cat]:

package require fileutil
foreach {first second} [split [fileutil::cat $filename] \n] {
# do whatever
}

Michael
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com