Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl not able to read file

by Anonymous Monk
on Sep 22, 2017 at 06:12 UTC ( [id://1199875]=perlquestion: print w/replies, xml ) Need Help??

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

The last line of the following program is not able to read the file Data file = MultiLineData.txt 19760 Austria 7800 Kingsland 124 Petrie Terrace 19762 19764 19765 19767 Austria 7864 Kingsland 1/249 Coronation Drive 19768 19770 Austria 7853 Kingsland Lawrence: 103 Frasers Rd 19771 19775 Austria 7800 Kingsland 127 Edward Street 19777 19779 19780 Austria 7963 Kingsland 133 King Street 19782 19784 19785 19787 19789 19791 Austria 7800 Kingsland Riverside Centre Level 29 123 Eagle Street 19793 19795 19796 19799 67301 67302 67304 Austria 7810 Kingsland Argyle Office Suit 9 20 Argyle Street 67306 67308
use strict; use warnings; my $dir = 'C:\Scripts'; open(my $fh1, '<', "$dir\\MultiLineData.txt") or die $!; open(my $out1, '>', "$dir\\work.txt") or die $!; open(my $fh2, '<', "$dir\\work.txt") or die $!; while (my $row = <$fh1>) { chomp $row; my $str = $row; $str =~ s/\t/|/g; print $out1 "$str\n"; } while (<$fh2>){chomp $_; print "$_\n"};

Replies are listed 'Best First'.
Re: Perl not able to read file
by choroba (Cardinal) on Sep 22, 2017 at 06:31 UTC
    If you want to read from a file you've just written to, you need to close the file after you're finished writing to it.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      It is probably not needed to close the filehandle. From the close documentation:
      You don't have to close FILEHANDLE if you are immediately going to do another open on it, because open closes it for you. (See open.)
      Having said that, I would still explicitly close the file handle before reopening it. This looks cleaner and more understandable.

      But the important point is that reopening the file for reading needs to be done after all required data has been printed to it.

      An additional comment on the OP code is that there is no need to chomp the input line if the next statement adds a \n to it.

        > You don't have to close FILEHANDLE if you are immediately going to do another open on it

        Yes, but there's no open going on the same filehandle, there is another filehandle opened for the same file .

        Try commenting the indicated line:

        #!/usr/bin/perl use warnings; use strict; open my $OUT, '>', '1' or die $!; open my $IN, '<', '1' or die $!; print {$OUT} "$_\n" for 1 .. 20; close $OUT or die $!; # <- Comment me! print while <$IN>;

        It's true that I'd rather open the filehandle later before reading from it, not at the top of the script, but even in such situation you need to close the output filehandle before reading from the file.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Perl not able to read file
by Corion (Patriarch) on Sep 22, 2017 at 06:31 UTC

    How do you know that it is unable to read the second file?

    What happens? What did you expect to happen?

    You cannot open the same file for reading and writing at the same time. If you want to read what you just wrote, you will also need to look at seek and flush to synchronize the two filehandles.

    Why do you want to read what you just wrote at all?

Re: Perl not able to read file
by thanos1983 (Parson) on Sep 22, 2017 at 08:55 UTC

    Hello Anonymous Monk,

    Fellow monks have provided you a solution to your problem. But why not use a module like File::Slurper that will do all the work for you in the background and possibly more efficiently also.

    Take a look I think it will help you to minimize your errors/problems.

    Update: I assume you want also to skip lines that do not have any valuable content, something like that?

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; use File::Slurper 'read_lines'; use Scalar::Util qw(looks_like_number); my @content = read_lines('in.txt'); print Dumper \@content; my @final; foreach my $line (@content) { $line =~s/\s+/ /g ; next if (looks_like_number($line)); push @final, $line; } print Dumper \@final; __END__ $ perl test.pl $VAR1 = [ '19760 Austria 7800 Kingsland 124 Petrie Ter +race', '19762 ', '19764 ', '19765 ', '19767 Austria 7864 Kingsland 1/249 Coronati +on Drive', '19768 ', '19770 Austria 7853 Kingsland Lawrence: 1 +03 Frasers Rd', '19771 ', '19775 Austria 7800 Kingsland 127 Edward Str +eet', '19777 ', '19779 ', '19780 Austria 7963 Kingsland 133 King Stree +t', '19782 ', '19784 ', '19785 ', '19787 ', '19789 ', '19791 Austria 7800 Kingsland Riverside Cent +re Level 29 123 Eagle Street', '19793 ', '19795 ', '19796 ', '19799 ', '67301 ', '67302 ', '67304 Austria 7810 Kingsland Argyle Office +Suit 9 20 Argyle Street', '67306 ', '67308' ]; $VAR1 = [ '19760 Austria 7800 Kingsland 124 Petrie Terrace', '19767 Austria 7864 Kingsland 1/249 Coronation Drive', '19770 Austria 7853 Kingsland Lawrence: 103 Frasers Rd', '19775 Austria 7800 Kingsland 127 Edward Street', '19780 Austria 7963 Kingsland 133 King Street', '19791 Austria 7800 Kingsland Riverside Centre Level 29 123 +Eagle Street', '67304 Austria 7810 Kingsland Argyle Office Suit 9 20 Argyle + Street' ];

    Then simply use File::Slurper/write_text($filename,_$content,_$encoding,_$crlf) and put your data into a file instead of my array that I placed in the code for demonstration purposes.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1199875]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-24 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found