Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

remove line feed at the end

by Anonymous Monk
on Aug 15, 2009 at 09:22 UTC ( [id://788849]=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp $_; if($_ !~ /^{/){ $_ =~ s/\s+//gi; $_ =~ s/\n//gi; $_ =~s/\r//gi; print "$_\n"; } } __DATA__ {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS.
I have tried all the ways to remove the new line character at the end of the line and store it in a single. Please tell me what is wrong in the above code. But no luck

Replies are listed 'Best First'.
Re: remove line feed at the end
by hda (Chaplain) on Aug 15, 2009 at 11:12 UTC
    You should write print "$_ "; instead of print "$_\n";

    Also, all the replacements within the if structure are not needed if you use "chomp", except one for getting rid of "\". See, for example, the following code:

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp; if($_ !~ /^{/){ $_ =~ s/\\//gi; chomp; print "$_ "; } } __DATA__ {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS.


    produces:

    author1 staff1 DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS.
      #!/usr/bin/perl use strict; use warnings; my $fh; my $tag; my %hash; my @metadata_tags; my $flag; while (<DATA>) { chomp $_; if(/^{(.*)}/) { $tag = $1; push(@metadata_tags,$tag); } else { if($tag eq 'FILE') { if(defined($fh)){ print $fh "</ROOT>"; close($fh); } my $filename = $_; open($fh, '>', "$filename.xml") or die "$filename: $!"; print $fh '<?xml version="1.0"?>',"\n"; print $fh "<ROOT>\n"; print $fh "<FILE>$filename</FILE>\n"; } elsif(defined($fh)) { if($_ ne ''){ if($_ !~ m/^{/){ $_ =~ s/\\//gi; chomp; print "<$tag>$_</$tag>\n"; # } } } } } } __DATA__ {FILE} sourcetag1 {NUMBER} 00000 11111 {SOURCE} source1 {KEYWORD} {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS. {FILE} sourcetag2 {NUMBER} 00002 {SOURCE} sourcenam2 {KEYWORD} {AUTHOR} author2 staff2
      But this produces output like this
      <NUMBER>00000</NUMBER> <NUMBER>11111</NUMBER> <SOURCE>source1</SOURCE> <AUTHOR>author1 staff1</AUTHOR> <HEADLINE>DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST</HEADLINE> <HEADLINE>STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS.</HE +ADLINE> <NUMBER>00002</NUMBER> <SOURCE>sourcenam2</SOURCE> <AUTHOR>author2 staff2</AUTHOR>
      Instead of
      <NUMBER>00000</NUMBER> <NUMBER>11111</NUMBER> <SOURCE>source1</SOURCE> <AUTHOR>author1 staff1</AUTHOR> <HEADLINE>DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST STYLE AT A SPE +ED USUALLY ASSOCIATED WITH WARDROBE ITEMS.</HEADLINE> <NUMBER>00002</NUMBER> <SOURCE>sourcenam2</SOURCE> <AUTHOR>author2 staff2</AUTHOR>
      Please anyone help me

        That's good. Looks like it works as designed.

        If you want to do special things when \ appears at the end of a line, then you should recognize it and do special things.

        You are only removing it from the line and then go on with the standard action...

        Consider something like this:

        #!/usr/bin/perl use strict; use warnings; my $output = ''; LINE: while ( my $line = <DATA>) { chomp $line; # if line ended with a '\', remove '\' and save line for later out +put if ( $line =~ s/\\$// ) { # clean indentation $line =~ s/^\s+/ /; # save line $output .= $line; next LINE; } # we have previous line(s) to consider? elsif ( length $output ) { # clean indentation $line =~ s/^\s+/ /; $line = $output . $line; $output = ''; } print $line, $/; } __DATA__ {FILE} sourcetag1 {NUMBER} 00000 11111 {SOURCE} source1 {KEYWORD} {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS +. {FILE} sourcetag2 {NUMBER} 00002 {SOURCE} sourcenam2 {KEYWORD} {AUTHOR} author2 staff2 a\ b\ c

        It's your exercise to combine this with your code ;o)

        BTW: why do you use the /i modifier, when replacing spaces, newlines or the backslash?

Re: remove line feed at the end
by bichonfrise74 (Vicar) on Aug 15, 2009 at 16:57 UTC
    Try this.
    #!/usr/bin/perl use strict; local $/ = '{'; while( <DATA>) { next if /^{/; s/\n|{/ /g; print "{$_\n"; } __DATA__ {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS.
Re: remove line feed at the end
by sanku (Beadle) on Aug 26, 2009 at 04:14 UTC
    The below code will convert the entire file as a single line.
    while (<DATA>) { chomp($_); print "$_"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2025-06-15 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.