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

Read block of file in Tcl
Hi,

I have an input file in the following format:
********************************
Name1
Name2
Row1 DEF 001
Row2 ABC DEG 003
Row3 EW EW 002
Row4 FF 0012
Row1 ADEF 1001
Row2 ARBC DEG 2003
Row3 EWW EW 0302
Row4 AB 0112
Name1
Name2
Row1 DSDEF 001
Row2 ABVC DEG 003
Row3 EDW EW 002
Row4 FVF 0012
Row1 AF 1001
Row2 ACRBC DEG 2003
Row3 ESWW EW 0302
Row4 AEB 0112
 ****************************************

I have to read this file and
--> if I get "Name1" in any of the line,
then I have to read the block of file from "Name1" to the first
occurrance
of "Row4".
--> If I get "Row1" after the above reading is done, then I have to
read
the block of file from "Row1" to the first occurrance of "Row4".

Can anyone help me out in doing this through TCL.

Thanks and regards,
Calvin


Report this thread to moderator Post Follow-up to this message
Old Post
calvin hobbes
05-26-05 01:59 PM


Re: Read block of file in Tcl
calvin hobbes wrote:
>
> Hi,
>
> I have an input file in the following format:
> ********************************
> Name1
> Name2
> Row1 DEF 001
> Row2 ABC DEG 003
> Row3 EW EW 002
> Row4 FF 0012
> Row1 ADEF 1001
> Row2 ARBC DEG 2003
> Row3 EWW EW 0302
> Row4 AB 0112
> Name1
> Name2
> Row1 DSDEF 001
> Row2 ABVC DEG 003
> Row3 EDW EW 002
> Row4 FVF 0012
> Row1 AF 1001
> Row2 ACRBC DEG 2003
> Row3 ESWW EW 0302
> Row4 AEB 0112
>  ****************************************

> I have to read this file and
>   --> if I get "Name1" in any of the line,
> then I have to read the block of file from "Name1" to the first
> occurrance
> of "Row4".
>   --> If I get "Row1" after the above reading is done, then I have to
> read
> the block of file from "Row1" to the first occurrance of "Row4".
>
> Can anyone help me out in doing this through TCL.
>
> Thanks and regards,
> Calvin

You question has the characteristics of a homework assignment.
Just a few hints:
- [gets] lets you read a file line by line
- the while loop may prove useful

Regards,

Arjen

Report this thread to moderator Post Follow-up to this message
Old Post
Arjen Markus
05-26-05 01:59 PM


Re: Read block of file in Tcl
Thanks Arjen.
This is what I wanted
while {gets $filename line}
{
if $line=Name1
{
read the file till $line=Row4
}
if $line=Row1
{
read the file till $line=Row4
}
}


But, how can the step "read the file till $line=Row4"  be achieved in
an optimal way.
And how will the file reading opeartion by the main while loop continue
after Row4 is found?


Report this thread to moderator Post Follow-up to this message
Old Post
nottycalvin
05-26-05 01:59 PM


Re: Read block of file in Tcl
nottycalvin wrote:
> But, how can the step "read the file till $line=Row4"  be achieved in
> an optimal way.
> And how will the file reading opeartion by the main while loop continue
> after Row4 is found?

Looking at your pseudocode, I see something like this:

set f [open $filename]
while {[gets $f line] > -1} {
if {$line eq "Name1"} {
while {[gets $f line] > -1 && $line ne "Row4"} {
# do something with $line maybe?
}
}
if {$line eq "Row1"} {
while {[gets $f line] > -1 && $line ne "Row4"} {
# do something with $line maybe?
}
}
}

And the above is fairly efficient; Tcl tries really hard behind the
scenes to optimize the low-level I/O for you.

Donal.

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


Re: Read block of file in Tcl
"Donal K. Fellows" <donal.k.fellows@manchester.ac.uk> wrote in message
news:d74167$f3d$1@godfrey.mcc.ac.uk...
> nottycalvin wrote: 
>
> Looking at your pseudocode, I see something like this:
>
>    set f [open $filename]
>    while {[gets $f line] > -1} {
>       if {$line eq "Name1"} {
>          while {[gets $f line] > -1 && $line ne "Row4"} {
>             # do something with $line maybe?
>          }
>       }
>       if {$line eq "Row1"} {
>          while {[gets $f line] > -1 && $line ne "Row4"} {
>             # do something with $line maybe?
>          }
>       }
>    }
>
> And the above is fairly efficient; Tcl tries really hard behind the
> scenes to optimize the low-level I/O for you.
>
> Donal.

I always liked this algorithm, unless the file is huge:

1. Read the whole file into variable with read
2. split the variable on "\n"
3. string first for "Name1"
4. string first for "Row4" starting at returned index from previous string
first
5. string range for the result



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


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 10:24 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.