Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

refine output of Dumper

by kaka_2 (Sexton)
on Oct 24, 2014 at 10:04 UTC ( [id://1104841]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, i have following code which works well. only problem is output does not looks good.
use strict; use warnings; use Data::Dumper; $Data::Dumper::Terse=1; my $StatChk="cat /tmp/teststatus.txt | grep -v OK"; my $GoodStat="OK"; my @SChk_Out; open (DChk, "$StatChk |") || die "Failed: $!\n"; @SChk_Out = <DChk>; close (DChk); print "Output of command is =".Dumper(@SChk_Out);
and Output is:
[ 'Station 7777: Not good. ', 'Station 7778: Not Good ' ]
how can i have output just
Station 7777: Not good. Station 7778: Not good.

it is important that i use the Dumper. in my actual program i cant just print array. (kind of limitation)

any help in this regard will be appriciated.

Replies are listed 'Best First'.
Re: refine output of Dumper
by choroba (Cardinal) on Oct 24, 2014 at 10:08 UTC
    To remove newlines, just use chomp:
    @SChk_Out = <DChk>; chomp @SChk_Out;

    BTW, your cat is useless. You can get rid of it as grep accepts filename as an argument:

    my $StatChk = 'grep -v OK /tmp/teststatus.txt';
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: refine output of Dumper
by McA (Priest) on Oct 24, 2014 at 10:13 UTC

    Hi,

    your question is a little bit weird. You want the output of the array line by line. In your array you do have every element ending in a newline character. So a simple

    print @SChk_Out

    should produce the output what you want. What is the problem?

    McA

Re: refine output of Dumper
by marto (Cardinal) on Oct 24, 2014 at 10:08 UTC

    "in my actual program i cant just print array. (kind of limitation)"

    Why?

      i can not say why? i work on a System Management tool which allow me to embedded perl codes in it but not exactly as we write perl program. i cant complie it outside the environment tool provides, and there is no way to print.

      Only thing i can do is using internal API's i can send the output to console and last time when i tried to send the output of array it did not work for me for that reason i had to use Dumper. Thanks.

      -KAKA-

        That sounds more like you cannot print to STDOUT, but can you print to STDERR?

        Can you warn?


        Enjoy, Have FUN! H.Merijn

        Do you send the Dumper output then to wherever (in place of printing yourself)?

Re: refine output of Dumper
by Loops (Curate) on Oct 24, 2014 at 10:19 UTC

    Hi there. I don't believe you about having to use Dumper ;o) And why not use Perl instead of relying on the external grep command?:

    my $StatChk='/tmp/teststatus.txt'; open my $DChk, '<', $StatChk || die "Failed: $!\n"; my @SChk_Out = grep {!/OK/} <$DChk>; close ($DChk); print @SChk_Out;
    Update: Okay I should have believed; my bad.
Re: refine output of Dumper
by RonW (Parson) on Oct 24, 2014 at 16:31 UTC
    print "Output of command is =\n".join('',@SChk_Out);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found