Q: Reading file in multiple times not working! 
Author Message
 Q: Reading file in multiple times not working!

Quote:

>I'm having trouble using the getline function to read in a flat-file more than
>once.  The first time I call the function, everything runs just fine.  The
>second time, nothing!  What am I doing wrong?
>Sample code:

>BEGIN {
>filename = "../files/data.txt"
>}
>{
>function readfile() {
>max_nf=0
>while ((getline < filename) > 0 ) {
>        if (max_nf < NF) {
>                max_nf = NF
>                for (i = 1; i <= NF; i++)
>                        tag[i] = $i
>                }
>        }
>        if (NR > 1)
>                total = total + $5
>        }
>close(filename)
>}
>END {
>readfile()
>print "The total is: " total
>}

The logic (and complexity) of your code is obscure. I am sure
what you are trying to do can be much more easily achieved.

If you want help just mail us ....

Meanwhile, I suspect that the answer to your question is that you have no
main body to your program just

BEGIN
function definition
END

Hence the function readfile is being called just once (in the END section
after all the files have been read)

Mark
--
Mark Katz
ISPC, London - Innovation in data-delivery tools
Tel: (44) 181-455 4665, Fax (44) 181-458 9554
** See our website at http://www.*-*-*.com/ **



Sat, 03 Feb 2001 03:00:00 GMT  
 Q: Reading file in multiple times not working!
[Posted and mailed]

Quote:

>I'm having trouble using the getline function to read in a flat-file more than
>once.  The first time I call the function, everything runs just fine.  The
>second time, nothing!  What am I doing wrong?

You have too many braces.  You cannot define a function inside braces,
functions are global and must be defined outside braces.  Your code is
essentially:

BEGIN { ... }
{
    function readfile() { ... }
    close(filename)

Quote:
}

END { ... }

The "function..." cannot be inside a set of braces -- it's not legal
awk in any version I am aware of.

Try this code, it was tested under Win95/Dos box (OSR1, 4.00.950a),
gawk v3.0.3 (DJGPP/DOS).  Note the use of "gNR" rather than "NR",
since NR is only updated when an external file is read, AFAIK.

Hope this helps.

Test command:
C:\>gawk -f read2x.awk NUL
The total is: 15
The total is: 34

------------ read2x.awk ------------------
BEGIN { filename = "read2x.txt" }
function readfile() {
    max_nf=0
    while ((getline < filename) > 0 ) {
        gNR++
        if (max_nf < NF) {
            max_nf = NF
            for (i = 1; i <= NF; i++)
                tag[i] = $i
        }
        if (gNR > 1)
            total = total + $5
    }
    close(filename)

Quote:
}

END {
    readfile()
    print "The total is: " total
    readfile()
    print "The total is: " total
Quote:
}

------------ read2x.awk ------------------

------------ read2x.txt ------------------
Line 1 2 3 4 5
Line 2 3 4 5 1
Line 3 4 5 10 2
------------ read2x.txt ------------------

----------------------------------------------------




Sun, 04 Feb 2001 03:00:00 GMT  
 Q: Reading file in multiple times not working!
% I'm having trouble using the getline function to read in a flat-file more than
% once.  The first time I call the function, everything runs just fine.  The
% second time, nothing!  What am I doing wrong?

Well, the code you posted has a syntax error, so I have to assume that
this is a failed attempt to simplify the thing that runs fine. I assume
that readfile() should be like this:

 function readfile() {
   max_nf=0
   while ((getline < filename) > 0 ) {
        if (max_nf < NF) {
                max_nf = NF
                for (i = 1; i <= NF; i++) {
                        tag[i] = $i
                }
        }
        if (NF > 1)
                total = total + $5
        }
  close(filename)
 }

That works perfectly well, however many times I run it against a file
containing
 1 2 3 4 5

If this isn't the function you're using, your problem could be with the
placement of braces, which might be putting close(filename) outside
the function.
--

Patrick TJ McPhee
East York  Canada



Sun, 04 Feb 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Multiple textures on one IndexFaceSet [multiple files, not a map]

2. Getting the same return string while reading multiple times from a GPIB instrument

3. Multiple field separators for paranthesis not working

4. problem sythesizing vhdl file/ucf file not working

5. VC++ obj file link to asm obj file with alink(not working)

6. timeout in multiple processes not working

7. Multiple Selection from a listbox: not working

8. multiple bindings does not work

9. Newbie Qs: startup time, Module.constants, chdir, Ruby distro's library

10. Qs re 'Write & 'Read

11. Filter on process or report does not work the second time

12. Print Panel.vi works with the full LabView version but not with Run Time Engine

 

 
Powered by phpBB® Forum Software