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

Hi..

I have a data as below:-

11111111111 222222222222 333333333 4444444444444
Then I want to remove all new line from the data and become one line, the output must be :-
111111111112222222222223333333334444444444444
Anyone could help me ?
I have tried a few method but its not work like I want.. below are sample of my code:

open (DATA, "$tmpfile"); while (my $data = <DATA>) { chomp($data); $data =~ s/\n+//g; #tr/\x0a\x0d//d; #s/\n//g; print "$data\n";

Replies are listed 'Best First'.
Re: How to remove all new line ?
by GrandFather (Saint) on Aug 18, 2008 at 02:00 UTC

    You are putting new line characters back with your print inside the loop. The loop handles one line at a time - it doesn't slurp the whole file. Probably the best fix is to move the print of the newline to after the read loop:

    open my $inData, '<', $tmpfile or die "Can't open $tmpfile: $!"; while (defined (my $data = <$inData>)) { chomp $data; print $data; } close $inData; print "\n";

    Note the use of the three parameter version of open and the result checking using die. Note too the use of a lexical for the file handle and that you don't need to interpolate the file name into a string.

    If you are assigning to a variable in the while loop condition you need to use defined to make the same test that Perl's magic while (<fh>) handling makes for you.

    The chomp should be all you need to strip line separators, but watch out for OS differences in line endings (if that is an issue).


    Perl reduces RSI - it saves typing
Re: How to remove all new line ?
by ysth (Canon) on Aug 18, 2008 at 01:51 UTC
Re: How to remove all new line ?
by CountZero (Bishop) on Aug 18, 2008 at 05:55 UTC
    How about?
    use strict; my @all_data = <DATA>; # read all data into the array chomp @all_data; # get rid of the newlines in each element of th +e array print @all_data; # print the whole array __DATA__ 11111111111 222222222222 333333333 4444444444444
    If your file is really huge you may run into memory problems as you keep it all in memory at the same time.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Which, incidentally can be done in one step:

      chomp(my @all_data = <DATA>);
      --
      If you can't understand the incipit, then please check the IPB Campaign.
        TIMTOWTDI++

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to remove all new line ?
by JavaFan (Canon) on Aug 18, 2008 at 10:26 UTC
    Here's a one-liner:
    perl -l0pe0 your_file
      Or this:
       print map { chomp; $_ } <DATA>;
      A reply falls below the community's threshold of quality. You may see it by logging in.

      I personally believe that however appealing this can be, to be very sure, perldoc perlrun recommends using 0777 rather than 0 because "there is no legal byte with that value."

      Update: JavaFan is actually right, since for some reason I was thinking of an entirely different problem, namely that of slurping, and not to the OP's. Which anyway would work with -0777 and not -l0777... Apologies!

      --
      If you can't understand the incipit, then please check the IPB Campaign.
        Using -l0777pe0 will not work. It will print ÿ after each line. (Now, -l0pe0 doesn't really work either, it prints a NUL byte after each line. But you won't see that...)
Re: How to remove all new line ?
by Anonymous Monk on Apr 15, 2019 at 11:26 UTC

    This is not very efficient way of doing it but it will work,

    @data = split("\n",$data); $data = join("",@data);