Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to get a program output into array

Reply
Thread Tools

How to get a program output into array

 
 
Thomas Barth
Guest
Posts: n/a
 
      11-13-2009
Hi,
any Idea how to get the output of this command into an array? The output
is still printed to the screen. The array @soxin keeps empty.

open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat |");
my @soxin = <SOXIN>;
close(SOXIN);

Thomas B
 
Reply With Quote
 
 
 
 
Thomas Barth
Guest
Posts: n/a
 
      11-13-2009
Thomas Barth schrieb:
> Hi,
> any Idea how to get the output of this command into an array? The output
> is still printed to the screen. The array @soxin keeps empty.
>
> open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat |");
> my @soxin = <SOXIN>;
> close(SOXIN);
>


Hi,
its cleared, I got it with the command
open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat 2>&1 |");

Thomas B



 
Reply With Quote
 
 
 
 
sharma__r@hotmail.com
Guest
Posts: n/a
 
      11-13-2009
On Nov 13, 7:31*pm, Thomas Barth <(E-Mail Removed)> wrote:
> Thomas Barth schrieb:
>
> > Hi,
> > any Idea how to get the output of this command into an array? The output
> > is still printed to the screen. The array @soxin keeps empty.

>
> > * * open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat|");
> > * * my @soxin = <SOXIN>;
> > * * close(SOXIN);

>
> Hi,
> its cleared, I got it with the command
> open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat 2>&1 |");
>
> Thomas B



Or this:

my @soxin = split /\n/, qx/ sox $path -r 8000 -c 1 $src_dir/
$basename.vox stat /;
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      11-13-2009
Thomas Barth <(E-Mail Removed)> wrote:
>Hi,
>any Idea how to get the output of this command into an array? The output
>is still printed to the screen. The array @soxin keeps empty.
>
> open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat |");
> my @soxin = <SOXIN>;
> close(SOXIN);


You could use simple backticks. But that shouldn't be much different
from the pipe. Maybe your sox command is writing to STDERR instead of
STDOUT?

jue
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-13-2009
>>>>> "sr" == sharma r <(E-Mail Removed)> writes:

sr> On Nov 13, 7:31*pm, Thomas Barth <(E-Mail Removed)> wrote:
>> Thomas Barth schrieb:
>>
>> > Hi,
>> > any Idea how to get the output of this command into an array? The output
>> > is still printed to the screen. The array @soxin keeps empty.

>>
>> > * * open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat |");
>> > * * my @soxin = <SOXIN>;
>> > * * close(SOXIN);

>>
>> Hi,
>> its cleared, I got it with the command
>> open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat 2>&1 |");
>>
>> Thomas B



sr> Or this:

sr> my @soxin = split /\n/, qx/ sox $path -r 8000 -c 1 $src_dir/
sr> $basename.vox stat /;

no need for the split. backticks/qx will split on \n in a list context.

also that won't work as you are using / for the delimiter and / is on
the data. so use another delimiter and {} is usually the best choice
there.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-14-2009
>>>>> "BM" == Ben Morrow <(E-Mail Removed)> writes:

BM> Quoth "Uri Guttman" <(E-Mail Removed)>:
>> >> its cleared, I got it with the command
>> >> open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat 2>&1 |");

>>

sr> Or this:
>>

sr> my @soxin = split /\n/, qx/ sox $path -r 8000 -c 1 $src_dir/
sr> $basename.vox stat /;
>>
>> no need for the split. backticks/qx will split on \n in a list context.
>>
>> also that won't work as you are using / for the delimiter and / is on
>> the data. so use another delimiter and {} is usually the best choice
>> there.


BM> Also, that won't work since qx// doesn't automatically do the 2>&1 that
BM> was missing in the first place...

yeah, i thought about that after i saw the other posts. it wasn't clear
from the OP that it was stderr coming out of sox (which is kind of odd
anyhow).

so the above code is wrong on 3 counts: no need for split, broken
delimiter and not redirecing stderr. not bad!

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      11-14-2009
On Nov 13, 6:00*am, Thomas Barth <(E-Mail Removed)> wrote:
> Hi,
> any Idea how to get the output of this command into an array? The output
> is still printed to the screen. The array @soxin keeps empty.
>
> * * *open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat |");
> * * *my @soxin = <SOXIN>;
> * * *close(SOXIN);
>


IPC::Run is another possibility:



use IPC::Run qw( run );
use strict ;
use warnings;

# ***** untested *****

run ["sox $path -r 8000 -c 1 $src_dir/$basename.vox stat"],
'<', \my @soxin, \my $err
or die "Markdown.pl failed: $?";

--
Charles DeRykus

 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      11-15-2009
On Nov 13, 10:04*pm, "C.DeRykus" <(E-Mail Removed)> wrote:
> On Nov 13, 6:00*am, Thomas Barth <(E-Mail Removed)> wrote:
>
> > Hi,
> > any Idea how to get the output of this command into an array? The output
> > is still printed to the screen. The array @soxin keeps empty.

>
> > * * *open(SOXIN, "sox $path -r 8000 -c 1 $src_dir/$basename.vox stat |");
> > * * *my @soxin = <SOXIN>;
> > * * *close(SOXIN);

>
> IPC::Run is another possibility:
>
> use IPC::Run qw( run );
> use strict ;
> use warnings;
>
> # ***** untested *****
>
> run ["sox $path -r 8000 -c 1 $src_dir/$basename.vox stat"],
> * * *'<', \my @soxin, \my $err
> * or die "Markdown.pl failed: $?";
>


Blech. A better attempt:

run ["sox ..."] , '<', \undef, '>&', \$out_and_err
or die "failed: $?";

But IPC::Run isn't the right solution for this task.

--
Charles DeRykus

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
pushing Find.find output into an array Jon Hawkins Ruby 2 08-03-2007 12:02 PM
write a single line C program whose output is the program itself Puneet C Programming 16 03-20-2005 08:15 AM
get meta information into xml output Rolf Kemper XML 2 09-13-2004 05:40 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM



Advertisments