Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Replace new line with white space

by llancet (Friar)
on Mar 10, 2014 at 01:40 UTC ( [id://1077641]=note: print w/replies, xml ) Need Help??


in reply to Replace new line with white space

You don't need regexp. You can just read a line each time, chomp the trailing "\n", then write it with a trailing space.

Replies are listed 'Best First'.
Re^2: Replace new line with white space
by Kenosis (Priest) on Mar 10, 2014 at 03:42 UTC

    It's difficult to know what the OP needs, even after the OP said, "...I read text from file into string...", since the following reads text from a file into a string, but chomp--in this case--will not remove newlines from $record:

    use strict; use warnings; local $/ = \1024; open my $fh, '<', 'records.txt' or die $!; while ( my $record = <$fh> ) { # process 1024-byte, fixed-length, newline-containing record } close $fh;
Re^2: Replace new line with white space
by fattahsafa (Sexton) on Mar 10, 2014 at 21:44 UTC
    Hi Man, great idea :) but I need to read the text from large number of huge files, so reading line by line may be not efficient.
      You probably don't have to worry about that. While your program may be reading line by line (presumably because that is what you need to do in your algorithm), behind the scenes, Perl is actually buffering input and reading larger chunks of data from the disk (possibly 4 or 8 kB at a time, depending on your OS, hardware, etc.). In brief, there is almost no penalty in reading your file line by line. I am using daily Perl programs to read GB or even dozens of GB of data, if it makes sense in the functional context to read data line by line, then just do it.
      No, it won't affect efficiency. Although you attempt to "read" line by line, the underlying library is actually reading block by block, and the <> operator parses the line and give you one line each time.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found