How to get a program output into array

Discussion in 'Perl Misc' started by Thomas Barth, Nov 13, 2009.

  1. Thomas Barth

    Thomas Barth Guest

    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
    Thomas Barth, Nov 13, 2009
    #1
    1. Advertising

  2. Thomas Barth

    Thomas Barth Guest

    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
    Thomas Barth, Nov 13, 2009
    #2
    1. Advertising

  3. Thomas Barth

    Guest

    On Nov 13, 7:31 pm, Thomas Barth <> 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 /;
    , Nov 13, 2009
    #3
  4. Thomas Barth <> 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
    Jürgen Exner, Nov 13, 2009
    #4
  5. Thomas Barth

    Uri Guttman Guest

    >>>>> "sr" == sharma r <> writes:

    sr> On Nov 13, 7:31 pm, Thomas Barth <> 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.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Nov 13, 2009
    #5
  6. Thomas Barth

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Quoth "Uri Guttman" <>:
    >> >> 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 ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Nov 14, 2009
    #6
  7. Thomas Barth

    C.DeRykus Guest

    On Nov 13, 6:00 am, Thomas Barth <> 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
    C.DeRykus, Nov 14, 2009
    #7
  8. Thomas Barth

    C.DeRykus Guest

    On Nov 13, 10:04 pm, "C.DeRykus" <> wrote:
    > On Nov 13, 6:00 am, Thomas Barth <> 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
    C.DeRykus, Nov 15, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chuck amadi
    Replies:
    1
    Views:
    490
    Larry Bates
    Jun 23, 2004
  2. Puneet
    Replies:
    16
    Views:
    1,126
    Daniel Vallstrom
    Mar 20, 2005
  3. Ron Main
    Replies:
    7
    Views:
    323
  4. Jon Hawkins
    Replies:
    2
    Views:
    111
    Robert Klemme
    Aug 3, 2007
  5. Deadolus
    Replies:
    6
    Views:
    120
    ara.t.howard
    Jan 22, 2008
Loading...

Share This Page