Problem Reading From File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sidra Nisar
    New Member
    • Feb 2010
    • 17

    Problem Reading From File

    hello...
    i have recently started learning perl.....and having issues when i try to read input from another file. the details of a script that i tried to run are as follows:

    i made a text file by the name “text.txt” and the contents were
    Sidra|38|BE
    Hira|48|BE
    Shagufta|50|BE

    Then i wrote the following script
    Code:
    open(DAT, "text.txt"); 
    $data_file=" text.txt ";
    open(DAT, $data_file); 
    @raw_data=<DAT>;
    close(DAT);
    foreach $student (@raw_data)
    {
     chomp($student);
     ($name,$roll_no,$class)=split(/\|/,$student);
     print "The student  $name bearing roll number $roll_no is in class $class";
     print "<br>\n";
    }
    the script produces no output and displays a message saying
    “readline () closed filehandle at <filename> line <line number>”
    I tried the same with another file by the name “text.dat” holding the same data but it did not work either. Please help me out resolving this issue.
    Thankyou...
    Last edited by numberwhun; Feb 5 '10, 01:35 PM. Reason: Please use code tags
  • Sidra Nisar
    New Member
    • Feb 2010
    • 17

    #2
    Readin Input From A File

    i have recently started learning perl. I am having a problem while reading input from a file. Kindly help me out to resolve this issue. The details of the script are as follows:

    i made a text file by the name “text.txt” and the contents were

    Sidra|38|BE
    Hira|48|BE
    Shagufta|50|BE

    Then i wrote the following script
    Code:
    open(DAT, "text.txt"); 
    $data_file=" text.txt ";
    open(DAT, $data_file); 
    @raw_data=<DAT>;
    close(DAT);
    foreach $student (@raw_data)
    {
     chomp($student);
     ($name,$roll_no,$class)=split(/\|/,$student);
     print "The student  $name bearing roll number $roll_no is in class $class";
     print "<br>\n";
    }
    The script produces no output and displays a message saying
    “readline () closed filehandle at <filename> line <line number>”
    I tried the same with another file by the name “text.dat” holding the same data but it did not work either. Please help me out.
    Thank you...
    Last edited by numberwhun; Feb 9 '10, 01:08 PM. Reason: Please use code tags!

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Sidra,

      There were a couple things wrong with your post, but one has earned you a one time warning. That issue is that you hijacked a thread on this forum. If you have a question, you absolutely do not ask that question as part of someone elses thread. That is called hijacking and is against the rules and guidelines of this forum. This is your one and only warning against this and next time will result in a temporary ban.

      The only other issue I had with your post was your lack of code tags use around the code you put in your post. Please use them as they are required.

      Regards,

      Jeff

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Please use the code tags when posting code.

        You should ALWAYS check the return code of an open call to verify that is was successful and take action if it failed. It is better to use the 3 arg form of open and a lexical var for the filehandle instead of a bareword.

        Every Perl script should include these 2 pragmas.
        Code:
        use warnings;
        use strict;
        Those pragmas will point out lots of simple mistakes, such as typos on vars, which could be difficult to track down otherwise. The strict pragma forces you to declare your vars, which is done with the 'my' keyword or in some rare cases with the 'our' keyword.

        Why are you trying to open the file twice?

        99% of the time you should loop over the data file line-by-line (or record-by-record instead of stuffing it into an array and then loop over that array.

        This is clearly a homework assignment so I won't provide a complete and corrected script, but I will give you a few lines, which will actually give you 90% of the solution. I'm leaving in 1 of your mistakes, but the added error handling will point you toward the answer.
        Code:
        my $data_file = " text.txt ";
        open my $DAT, '<', $data_file or die "can't open <$data_file> $!";
        
        while (my $student = <$DAT>) {

        Comment

        • Sidra Nisar
          New Member
          • Feb 2010
          • 17

          #5
          Jeff,

          I am grateful to you for pointing out my mistake. Please accept my apologies for it.

          Regards,
          Sidra

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            What you are doing is a bit cludgy at best and certainly needs a bit of changes.

            First, when opening a file, you want to not only specify whether you are reading from or writing to it, but also want to know if something happened while opening it.

            So, you could re-write your file opening statement like this:

            Code:
            open(DAT, "<text.txt") or die "Could not open file: $!";
            The $! prints out the error saying why it couldn't open the file.

            I recommend you read the open help page on perldoc.

            Also, one other thing to note, you have top open() statement, both using the same FILEHANDLE. This is a no-no. Always use a different file handle for different files.

            Regards,

            Jeff

            Comment

            • Sidra Nisar
              New Member
              • Feb 2010
              • 17

              #7
              I tried the following code:
              Code:
              #!/usr/bin/perl -w
              $data_file=" text.txt "; 
              open(DAT, $data_file) or die"terminate the file: $!"; 
              @raw_data=<DAT>; 
              close(DAT); 
              foreach $student (@raw_data) 
              { 
               chomp($student); 
               ($name,$roll_no,$class)=split(/\|/,$student); 
               print "The student  $name bearing roll number $roll_no is in class $class"; 
               print "\n";
              }
              Now it is giving this error message:

              Name "main::data_fil e" used only once: possible typo at ./file1.pl line 2
              terminate the file: No such file or directory at ./file1.pl line 3
              Last edited by numberwhun; Feb 10 '10, 04:37 AM. Reason: PLEASE USE CODE TAGS!!!

              Comment

              • Sidra Nisar
                New Member
                • Feb 2010
                • 17

                #8
                this isn't helping either.i get the error " no such file or directory at file1.pl line3...but i do have the file text.txt written in the same directory and i have created it musing mkdir.. is there another command i should use to create "files"...

                please direct me how to open this text.txt file .. i will make the changes to the script as you suggested myself..
                thanks

                Comment

                • RonB
                  Recognized Expert Contributor
                  • Jun 2009
                  • 589

                  #9
                  Take a closer look at the error message.

                  Putting the filename inside < > should have clued you in to the fact that ' text.txt' ( note the leading space ) is not the same filename as 'text.txt'.

                  Comment

                  • Sidra Nisar
                    New Member
                    • Feb 2010
                    • 17

                    #10
                    This might be a typing mistake here.
                    in my script, i used it as "text.txt". ...
                    i executed the following script:
                    Code:
                    #!/usr/bin/perl -w 
                    $data_file="text.txt ";  
                    open(DAT, $data_file) or die"terminate the file: $!";  
                    @raw_data=<DAT>;  
                    close(DAT);  
                    foreach $student (@raw_data)  
                    {  
                     chomp($student);  
                     ($name,$roll_no,$class)=split(/\|/,$student);  
                     print "The student  $name bearing roll number $roll_no is in class $class";  
                     print "\n"; 
                    }
                    This code is giving the error message
                    Please guide me how to open a file.

                    Regards.
                    Last edited by numberwhun; Feb 10 '10, 06:15 PM. Reason: PLEASE USE CODE TAGS!!!!!

                    Comment

                    • RonB
                      Recognized Expert Contributor
                      • Jun 2009
                      • 589

                      #11
                      in my script, i used it as "text.txt"
                      No you didn't.

                      The script now has a trailing space in the filename but the filename you just stated doesn't.

                      Comment

                      • chaarmann
                        Recognized Expert Contributor
                        • Nov 2007
                        • 785

                        #12
                        ...
                        and at line 3 you forgot to put a single space after "die", before the double quotation mark.

                        Spaces are important! You should always check them.

                        Please use always code-tags around your code!

                        Comment

                        • numberwhun
                          Recognized Expert Moderator Specialist
                          • May 2007
                          • 3467

                          #13
                          Sidra Nisar,

                          This is the fourth time in this thread that I have had to add code tags around the code that you have posted in this forum. Code tags are required around code you put in your posts, just as they are on a lot of sites. If you do not put them in, then we have to clean up behind you.

                          You need to put them in your posts around your code. If you are unaware of how to use code tags, then please see the help section that refers to them.

                          Regards,

                          Jeff

                          Comment

                          • Sidra Nisar
                            New Member
                            • Feb 2010
                            • 17

                            #14
                            Thank you so much for your help. I am now able to read files and working more on it.
                            Regards.

                            Comment

                            • Sidra Nisar
                              New Member
                              • Feb 2010
                              • 17

                              #15
                              ............... ............... ............... ............... ............... ............... ............... ............... ......

                              Comment

                              Working...