How to get a program output into array

T

Thomas Barth

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
 
T

Thomas Barth

Thomas said:
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
 
S

sharma__r

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 /;
 
J

Jürgen Exner

Thomas Barth said:
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
 
U

Uri Guttman

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
 
U

Uri Guttman

sr> my @soxin = split /\n/, qx/ sox $path -r 8000 -c 1 $src_dir/
sr> $basename.vox stat /;
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
 
C

C.DeRykus

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: $?";
 
C

C.DeRykus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top