Problem reading a text file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thanawala27
    New Member
    • Mar 2007
    • 68

    Problem reading a text file.

    Hi,

    I'm facign a strange problem in reading a text file.

    The contents of my text file is:

    A1;B1;C1;D1
    A2;B2;C2;D2
    A3;B3;C3;D3


    i want to split the contents based on the ";" (semi-colon).

    I use the following code for it:

    Code:
    open (OUT, "report.txt") || die "Can't open the File $!\n";
    my $data = <OUT>;
    
    
    my @values = split ";", $data;
    my $num = 0;
    
    while($num < 12)
    {
    	
    	print $values[$num++];
    	print "<br>";
    	
    }
    close(OUT);
    
    print "DONE!";
    Im getting the following output:
    --------------------------------
    A1
    B1
    C1
    D1







    DONE!
    ---------------------------------

    What surprises me, is why the remaining contents of $data is not stored in the "$values" array.

    Another Question: if i try to split $data like this:

    Code:
    my @values = split "\n", $data;
    then, the output is:
    ------------------------------
    A1;B1;C1;D1


    DONE!
    ------------------------------

    Can any1 tell me what could be the reason for the text after D1 not being displayed in both the cases.


    Any help is appreciated.


    Thanks,


    Ravi
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    all this does is read the first line of the file:

    Code:
    my $data = <OUT>;
    thats all your code ever does, is read the first line, process the first line, and ends.

    You can do this to read the entire file into an array and then loop through the array to process the lines:

    Code:
    my @data = <OUT>;
    
    for (@data) {
       .....
    }
    or process the file line by line:

    Code:
    while (my $data = <OUT>) {
       .....
    }

    Comment

    • thanawala27
      New Member
      • Mar 2007
      • 68

      #3
      Hey Kevin,

      It works and fits my requirement too.

      Thanks a lot.


      Ravi

      Comment

      Working...