Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 text files. My first file is my primary file which I'm reading data from. I have a condition that if it's true I have to open another file and get the line of data that goes with the first file text. My problem is I can read a text file within a file but I can't go back to my main file. I only get the first line and the second line involves me going into the other file but doesn't go back to the first file. I only want the read the line of the second file only when the condition is true then go to the second file and get the data.

Replies are listed 'Best First'.
Re: Multiple text files
by boo_radley (Parson) on May 27, 2003 at 23:56 UTC

    My problem is I can read a text file within a file but I can't go back to my main file
    Man, that doesn't make any sense. It sounds like you're saying once you read a line from the second file, your access to the main text file is revoked, or the main file gets closed... You do know you can have multiple file handles open in perl, right?

      Yes I do know I can have multiple file handles. Let me try to explain this another way. I have file1 which is the main file. This file is to stay open always. I only access a line in file2 if this condition exist:

      if what I'm reading in file1 is true, then I go to file2 and get that line of data. if it's false I want to stay in file1 and print my output.

      The problem is once I open file2 it reads the whole file and never goes back to file1. When I look at my output, it prints the first line of file1 because the condition is false. Then it prints the first line of file2 b/c the condition is true. So it goes to the file but it reads and prints the whole file. It doesn't check the next line of data from file1. I never see when the condition is false and stay in file1 again.
        Thanks for your help. I got it!!!!
Re: Multiple text files
by Anonymous Monk on May 27, 2003 at 22:54 UTC

    Show us!

      file1.txt
      Washington,Louise,Mary,6,H,193 First St.
      Washington,Ann,Carla,1,C,187 10th St.
      file2.txt
      Child,28,209-568-5177

      file1 includes: last name,middle name, first name,# of children,parent code,address
      file2 includes: child,age,address

      What I'm trying to do is read line one of the file1.txt file if parent code is 'H' then I print that line as is but if parent code is 'C' then I include the information from file1 and go to file2 and retrieve that information for the child which is in the file2 file. So what my output looks like is:

      Washington,Louise,Mary,6,H,193 First St.
      Washington,Ann,Carla,1,C,187 10th St.,Child,28,209-568-5177


      I have to go back to file1 which has infinite number of lines of data and read it and go to file2 only when it's a child and get that information to add to file1.

        He meant show us the code...

        My guess, even without the code, is that you are re-opening the same handle. You'll want to use a different handle for your second file. I.e. if your first open is something like open(FOO, "<file.txt") the next open shouldn't reuse FOO.

        -sauoq
        "My two cents aren't worth a dime.";