Home > Archive > Tcl > June 2005 > Read block of file in Tcl
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 |
Read block of file in Tcl
|
|
| calvin hobbes 2005-05-26, 8:59 am |
| 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
| |
| Arjen Markus 2005-05-26, 8:59 am |
| 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
| |
| nottycalvin 2005-05-26, 8:59 am |
| 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?
| |
| Donal K. Fellows 2005-05-26, 8:59 am |
| 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.
| |
| Ray Mosley 2005-06-03, 3:58 am |
|
"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
|
|
|
|
|