in reply to Re: Number of lines from a system call
in thread Number of lines from a system call

Ive tried using backticks I think its not interpreting the $server variable. in this test script its fine
#!/usr/bin/perl -w use strict; use diagnostics; use Data::Dumper; my $server="rprint1"; my $num = qx{/usr/bin/lpstat -h $server -W not-completed|wc -l}; print Dumper "$num"; my $server1='rlinux1'; my $num2 = qx{/usr/bin/lpstat -h $server1 -W not-completed|wc -l}; print Dumper "$num2";
produces
$VAR1 = 'rprint1'; $VAR1 = '2 '; $VAR1 = 'rlinux1'; $VAR1 = '3 ';
but from the original script
print Dumper "$server"; my $server_num_jobs = qx{/usr/bin/lpstat -h $server -W not-compl +eted|wc -l}; print Dumper "$server_num_jobs";
produces
$VAR1 = 'rprint1'; $VAR1 = '0 '; $VAR1 = 'rlinux1'; $VAR1 = '0 ';

Replies are listed 'Best First'.
Re^3: Number of lines from a system call
by betterworld (Curate) on Mar 16, 2005 at 20:57 UTC
    If you use backticks in scalar context, like you do, you'll get the whole output in one string. If you use them in list context, you'll get the lines of output as a list.
    So what you need is:
    my $num = () = qx{/usr/bin/lpstat -h $server -W not-completed};
    which puts the number of lines output by the command into $num.
    This syntax puts qx{...} into list context and returns the number of elements in the list rather than the list itself.

    Besides, why are you using Data::Dumper? This is nice for structured variables, but for plain strings it's more straightforward to write:

    print "number is: $num\n";
      Thanks.
      I was using Data::Dumper earlier in my code to dump and array, and I just continued using it.