On Thu, 27 Dec 2007 10:46:42 -0500, Clifton Lee wrote:
> The output from the system command may be better stored in a scalar and then
> split into an array (separated by return line characters).  Then you can use
> the 'foreach' operator for each element in your array.

That makes no sense.  Perl will supply a list of lines when you call the
backticks operator (not system command) in list context; why add the
unnecessary step?

> @arr1 = qw/java oracle/;
> $scalar = `ps -eo pid,user,pcpu,pmem,args | grep -v grep | grep $arr1[1]`;
> @arr2 = split(/\n/,$scalar);
> $length = $#arr2;

$length is not used.

> foreach my $val (@arr2)
> {
>     # get rid of the leading space for each line if one exists 

split can do that for you.

>     $val =~    s/^\s//;
> 
>     @arr = split(/\s+/,$val);
>     print $arr[1]."\n";
> }

my @arr1 = qw/java oracle/;
for (`ps -eo pid,user,pcpu,pmem,args | grep -v grep | grep $arr1[1]`)
{
  my @arr = split;
  print "$arr[1]\n";
}

That strictly matches the original code, but John's embellishments of
limiting the split fields and searching only the user field make it
better.  One could also use Proc::ProcessTable from CPAN to remove the
dependency on the ps program (questionable benefit).

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to