Loading ...
Sorry, an error occurred while loading the content.

re: Help...reading a file and open files based on list.

Expand Messages
  • markev_123
    I can t figure out what I m doing wrong. When I run my script I get the file listing from the external file. Whats not happening is I want to read in the first
    Message 1 of 3 , Dec 1, 2004
    • 0 Attachment
      I can't figure out what I'm doing wrong. When I run my script I get
      the file listing from the external file. Whats not happening is I
      want to read in the first file then open the file and output the
      contents. Then loop to the next file in the list, display, and so
      on...

      All I get from the code below is just the contents of the file
      listing from the external file.

      filename.in
      ===========

      11222.txt
      11333.txt
      11444.txt

      So I read in filename.in Then I want to open the other external file
      11222.txt and output this using "print". Then close the file and open
      11333.txt and so on...

      open XFILE, "< filename.in" or die qq(Cannot open file test.pl: $!);

      @filecontent = <XFILE>;
      foreach $line (@filecontent) {

      open INFILE, "< $line" or die qq(Cannot open
      file "$line": $!);
      print INFILE;
      close (INFILE);

      print "$line";
      }
      close (XFILE);
    • Jonathan Paton
      Hi, Debugging is a skill learned by doing, so I am only going to give hints. For each filename, you are doing: open INFILE,
      Message 2 of 3 , Dec 1, 2004
      • 0 Attachment
        Hi,

        Debugging is a skill learned by doing, so I am only going to give hints.

        For each filename, you are doing:

        open INFILE, "< $line" or die qq(Cannot open
        file "$line": $!);
        print INFILE;
        close (INFILE);

        print "$line";

        Now image nothing happened between that open and that close. What
        would you get? And why?

        Jonathan Paton
      • buxh42a
        ... You are getting the contents of filename.in because you are printing it yourself with print $line ; You really shouldn t expect to see the contents of a
        Message 3 of 3 , Dec 1, 2004
        • 0 Attachment
          --- In perl-beginner@yahoogroups.com, "markev_123" <markev_123@y...>
          wrote:
          >
          >
          >
          >
          > open XFILE, "< filename.in" or die qq(Cannot open file test.pl: $!);
          >
          > @filecontent = <XFILE>;
          > foreach $line (@filecontent) {
          >
          > open INFILE, "< $line" or die qq(Cannot open
          > file "$line": $!);
          > print INFILE;
          > close (INFILE);
          >
          > print "$line";
          > }
          > close (XFILE);

          You are getting the contents of filename.in because you are printing
          it yourself with 'print "$line"';

          You really shouldn't expect to see the contents of a file by just
          printing the filehandle variable - print INFILE; is not going to do
          it. Try this:


          open XFILE, "< filename.in" or die qq(Cannot open file test.pl: $!);

          @filecontent = <XFILE>;
          #you can close XFILE here since you don't need it anymore
          close (XFILE);

          foreach $line (@filecontent)
          {
          chomp ($line);
          open INFILE, "<$line" or die qq(Cannot open file "$line": $!);
          @printit = <INFILE>;
          #close it first, since you may have some buffer issues
          close (INFILE);
          print @printit;
          }


          This should work. Also, try using "use strict" in the future, this
          may help you cut down on any hidden bugs.

          Sam
        Your message has been successfully submitted and would be delivered to recipients shortly.