Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

cat the file and put it in variables
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


Report this thread to moderator Post Follow-up to this message
Old Post
Nitin
06-06-05 08:58 AM


Re: cat the file and put it in variables
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


Report this thread to moderator Post Follow-up to this message
Old Post
abhishekkabra@gmail.com
06-06-05 08:58 AM


Re: cat the file and put it in variables
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.

Report this thread to moderator Post Follow-up to this message
Old Post
SM Ryan
06-06-05 01:57 PM


Re: cat the file and put it in variables
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Donal K. Fellows
06-06-05 01:57 PM


Re: cat the file and put it in variables

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


Report this thread to moderator Post Follow-up to this message
Old Post
Khaled
06-06-05 01:57 PM


Re: cat the file and put it in variables
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Cameron Laird
06-06-05 09:00 PM


Re: cat the file and put it in variables
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 durin
g
>all input and output operations.

Report this thread to moderator Post Follow-up to this message
Old Post
Cameron Laird
06-06-05 09:00 PM


Re: cat the file and put it in variables
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

Report this thread to moderator Post Follow-up to this message
Old Post
Michael A. Cleverly
06-10-05 09:02 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Tcl archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:45 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.