RE: beginners-help@perl.org
- From: Peter@xxxxxxxx (Peter Scott)
- Date: Fri, 28 Dec 2007 05:38:50 -0800
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/
.
- References:
- RE: beginners-help@perl.org
- From: Clifton Lee
- RE: beginners-help@perl.org
- Prev by Date: Re: how to accept array elments using loop
- Next by Date: Re: Problem with adding a connection with Win32::NetResource
- Previous by thread: Re: beginners-help@perl.org
- Next by thread: FTP file handle question
- Index(es):
Relevant Pages
|