Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Perl Script New Line problem

by rajkrishna89 (Acolyte)
on Dec 21, 2011 at 08:44 UTC ( #944557=perlquestion: print w/replies, xml ) Need Help??
rajkrishna89 has asked for the wisdom of the Perl Monks concerning the following question:

I have written a script which will search for a particular tag and when its found the next line wil be printed..the script works fine but its coming in a straight line ..
use strict; use warnings; use Win32::OLE; use Win32::OLE qw(in with); use Win32::OLE::Variant; use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Const 'Microsoft Word'; use Cwd; use File::Find; use Win32::OLE; use Win32::OLE::Enum; $Win32::OLE::Warn = 3; # die on errors. +.. my $out_file = 'output.txt'; open my $out_fh, '>', $out_file or die "Could not open file $out_file: +$!"; my $print_next = 0; #Globals our $Word; our $reviewchklists; my @scriptfiles; @scriptfiles=glob('*.doc'); foreach my $file (@scriptfiles) { my $var; my $filename = "D\:\\"; $var = $filename."$file"; print $var ; my $document = Win32::OLE -> GetObject("$var"); print "Extracting Text ...\n"; my @array; my $paragraphs = $document->Paragraphs(); my $enumerate = new Win32::OLE::Enum($paragraphs); while(my $paragraph = $enumerate->Next()) { my $text = $paragraph->{Range}->{Text}; $text =~ s/[\n\r\t]//g; $text =~ s/\x0B/\n/g; $text =~ s/\x07//g; chomp $text; my $Data .= $text; @array=split(/\.$/,$Data); foreach my $line( @array) { if ($print_next) { print $out_fh $line ; #No need to chomp - we print t +he "\n" } $print_next = ($line =~ /^Customer\sID : \sYes/); } } } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The output is : ID :123 ID :257 ID :545 ID: 589

I want it to be printed as

ID :123

ID :257

ID :545

ID: 589

Replies are listed 'Best First'.
Re: Perl Script New Line problem
by Ratazong (Monsignor) on Dec 21, 2011 at 08:54 UTC
    Simple. Change
    print $out_fh $line ; #No need to chomp - we print the "\n"
    print $out_fh $line."\n" ; # we add a "\n"
    HTH, Rata
Re: Perl Script New Line problem
by Anonymous Monk on Dec 21, 2011 at 08:53 UTC

    Are you serious?

    If this part of the code doesn't print a newline

    print $out_fh $line ; #No need to chomp - we print the "\n"

    Maybe $line contains no newline, maybe you need to print one


    $line =~ s/\s+$//; $line = "$line\n";
Re: Perl Script New Line problem
by perl.j (Pilgrim) on Dec 21, 2011 at 15:48 UTC

    If you are using Perl v5.12 and up, you can simply say

     say $out_fh $line

    say automatically adds a newline


      not by default, but only if you enable modern perl, say through Modern::Perl or -E

      $ perl -e " say 1 Number found where operator expected at -e line 1, near "say 1" (Do you need to predeclare say?) syntax error at -e line 1, near "say 1" Execution of -e aborted due to compilation errors. $ perl -E " say 1 1
        Or he can simply say use 5.12.x.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://944557]
Approved by marto
[LanX]: more often for some weeks now
[Corion]: I think I'll have to manually (as god) intervene with that node, as the simple reparenting didn't seem to fix the parent/child relationship of the nodes
[Corion]: I think I have an idea but I'll have to open a ticket with on that - hopefully I get to that on the weekend
LanX imagines a burning thorn bush
[Mr. Muskrat]: Thank you!
[Mr. Muskrat]: Oh that is odd. I got the message that it was reparented but yeah, it didn't actually do it. lol
[Corion]: LanX: No, Co-Rion appears as Monkey God :-)
[Corion]: Mr. Muskrat: Yeah, that message is automatic when the "normal" reparenting is done, but I think some rows that make up the node were not added to the DB properly

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2017-01-19 16:28 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.