Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why do I only get 1 line of data output

Status
Not open for further replies.

itsmythg

Technical User
Apr 4, 2001
34
US
I am working on collecting system usage data. I have the following perl script that works, except it only reports the last line of data only.

sub getPsefData {
open(PSEF_PIPE,"ps -ef |");
$i=0;
while (<PSEF_PIPE>) {
chomp;
@psefField = split(' ', $_, 8);
$pid[$i] = $psefField[1];
$uid{$pid[$i]} = $psefField[0];
$ppid{$pid[$i]} = $psefField[2];
($min,$sec) = split(/:/,$psefField[6]);
$time{$pid[$i]} = $min * 60 + $sec;
$args{$pid[$i]} = $psefField[7];
$i++;
}
close(PSEF_PIPE);
}


#------------------------- Main ---------------------------#

#------------------------------------------------------------#
# Step 1: Get the ps -ef data, and put it in the desired #
# arrays and hashes. #
#------------------------------------------------------------#
&getPsefData;

{
print &quot;pid $psefField[1], uid $psefField[0], ppid $psefField[2],time $min $sec,args $p
sefField[7]\n&quot;;
}
 
The problem is that you are only printing the data from the last line you parsed and stored. What you need is a for loop that loops thru the arrays and hashes you created in getPsefData and prints the data from them. For example:
Code:
for my $i (0..$#pid) {
   print &quot;pid $pid[$i], uid $uid{$pid[$i]}, ppid $ppid{$pid[$i]}, time $time{$pid[$i]}, args $args{$pid[$i]}\n&quot;;
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top