Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to print bunch of data in a file without breaking in to new line

by Udhay (Initiate)
on Jul 31, 2012 at 15:46 UTC ( [id://984612]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I'm new to the perl development. I need to print a bunch of data stored in a variable without breaking in to new line in a file. Please help me to solve this problem.

  • Comment on How to print bunch of data in a file without breaking in to new line

Replies are listed 'Best First'.
Re: How to print bunch of data in a file without breaking in to new line
by toolic (Bishop) on Jul 31, 2012 at 15:49 UTC

      Hi,

      Thanks, for your quick response.

      I have stored all the contents of a HTML file in a variable. While i'm printing the data in a file, the content breaks in to new line exactly where the closing tag </p> or </span> or </div> appears, but it should not.

        It can get confusing with html documents, because the html bracket return acts as a newline. Here is a simple way to clean your html.
        #!/usr/bin/perl use warnings; use strict; open (FH,"< zz.html") or die "$!\n"; read( FH, $buf, -s FH ); close FH; #print "$buf\n"; $buf =~ s/\n//g; # strips out all newlines open (OH,"> zz_out.html") or die "$!\n"; print OH $buf; close OH; # zz_out.html will not have any newlines when viewed with a text edito +r, but will still be viewable in a web browser

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: How to print bunch of data in a file without breaking in to new line
by zentara (Archbishop) on Jul 31, 2012 at 16:28 UTC
    Since you are new to Perl, here is a simple script. Perl only prints a newline if you tell it to. The following prints the numbers 1 to 1000 all on one line, then prints some text on the next line.
    #!/usr/bin/perl use warnings; use strict; my $numberstring = join ' ', (1..10000); print $numberstring; # printed without a newline print "\n"; # print a newline print "ciao\n"; #printed with a newline

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: How to print bunch of data in a file without breaking in to new line
by Rudolf (Pilgrim) on Jul 31, 2012 at 16:49 UTC

    You say you have stored the entire file into one variable, but perhaps your generalizing? Can you show how you stored it? for example: did you store it in a scalar ($var) or list (@var), and how did you print it to the file? perhaps when you took the html from whatever source, it contained newlines, to be sure there are none you can use

     chomp()

    in a loop to write to this new file

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-18 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found