RE: beginners-help@perl.org



Hello,

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.

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

foreach my $val (@arr2)
{
# get rid of the leading space for each line if one exists
$val =~ s/^\s//;

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


*******************************************************************************
The views, opinions, and judgments expressed in this message are solely those of the author. The message contents have not been reviewed or approved by the UFT Welfare Fund.
*******************************************************************************
.



Relevant Pages

  • Re: str_ireplace in a foreach not working right.
    ... Try using a reference in the outer foreach instead: ... $gobbledegook_alphabet = array ... Your script was creating a fresh copy of the string on each iteration. ... of characters with certain key characters in it, ...
    (comp.lang.php)
  • Re: Reading text file in reverse order
    ... Did you understand the hint I gave earlier? ... foreach my $lin{ ... array contains all lines in the file; each element in the array is a ... characters into an array so you can reverse them. ...
    (perl.beginners)
  • Re: passing a list as a variable
    ... I would like to pass this list directly into a foreach function. ... problem is, when i pass my scalar in, i get: ... Argument "0..15,33..43,100..111" isn't numeric in array element ...
    (perl.beginners)
  • Re: empty displayed data except "space" and "newline"
    ... For identifying whether it's an 'empty displayed' value, ... foreach my $k_tmp{ ... Or you could push*and* clear out the array at the same time: ... and $k_tmp contains space characters at the beginning like " some ...
    (perl.beginners)
  • A Question
    ... the array ... foreach my $scalar { ...
    (comp.lang.perl.misc)