What would be the best data structure to keep these values

2003-12-16 Thread Hemond, Steve
Hi people,

I want trap the results of 'ps -ef' command.

I first trap the results by splitting the results in scalar values such as $uid $pid 
$ppid $stime, etc...

I would like, for each line returned, to hold these values in an array, so that, for 
each uid, I could return its corresponding pid, ppid, stime, cmd, etc.

What would be the best data structure to use? An hash table in an array? ...

Thanks in advance,

Best regards,

Steve Hemond
Programmeur Analyste / Analyst Programmer
Smurfit-Stone, Ressources ForestiÃres
La Tuque, P.Q.
Tel.: (819) 676-8100 X2833
[EMAIL PROTECTED] 



Re: What would be the best data structure to keep these values

2003-12-16 Thread Ricardo SIGNES
* "Hemond, Steve" <[EMAIL PROTECTED]> [2003-12-16T10:13:31]
> I want trap the results of 'ps -ef' command.
> 
> I first trap the results by splitting the results in scalar values
> such as $uid $pid $ppid $stime, etc...
> 
> I would like, for each line returned, to hold these values in an
> array, so that, for each uid, I could return its corresponding pid,
> ppid, stime, cmd, etc.
> 
> What would be the best data structure to use? An hash table in an
> array? ...

The answer really really depends on what you want to do with the data.
If you just want to print it back out, an array of arrayrefs is
simplest.  If you want to query specific fields line-by-line, an array
of hashrefs might be best.  If you want to look things up by pid, a hash
of hashrefs might be best.

In order, those would be something like:

# LOL - list of lists
foreach $line (@ps_results) { push @lines, [ split /\s+/, $line ] }

# LOH - list of hashes
foreach $line (@ps_results) { 
@results{uid pid stime} = split /\s+/, $line; # this line is pseudo
push @lines, \%results;
}

# HOH - hash of hashes
foreach $line (@ps_results) {
@results{uid pid stime} = split /\s+/, $line; # this line is pseudo
%lines{$results->{pid}} = \%results;
}

I'd probably use a HOH, because accessing the data would be easy to
write and because *I'd* probably only be /reading/ the data if I thought
I'd later need to access it by pid and field name.

-- 
rjbs


pgp0.pgp
Description: PGP signature


Re: What would be the best data structure to keep these values

2003-12-16 Thread Rob Dixon
Steve Hemond wrote:
>
> I want trap the results of 'ps -ef' command.
>
> I first trap the results by splitting the results in scalar values such as $uid $pid 
> $ppid $stime, etc...
>
> I would like, for each line returned, to hold these values in an array, so that, for 
> each uid, I could
> return its corresponding pid, ppid, stime, cmd, etc.
>
> What would be the best data structure to use? An hash table in an array? ...

Hey Steve.

Depending on what you want to do with the data, I would just keep a hash relating
UID to the entire 'ps' output record:

  my %ps = map { /(\S+)/; ($1, $_) } `ps -ef`;

and then split the record after later extracting it from the hash.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: What would be the best data structure to keep these values

2003-12-16 Thread Wiggins d Anconia


> Hi people,
> 
> I want trap the results of 'ps -ef' command.
> 
> I first trap the results by splitting the results in scalar values
such as $uid $pid $ppid $stime, etc...
> 
> I would like, for each line returned, to hold these values in an
array, so that, for each uid, I could return its corresponding pid,
ppid, stime, cmd, etc.
> 
> What would be the best data structure to use? An hash table in an
array? ...
> 

Per my usual, no fun for anyone response, I would dispense with shelling
out to ps, and use Proc::ProcessTable as your command is not cross
platform compatible, error prone, slow, etc.  then use its standard
interface for retrieving the values you need when you need them...

http://danconia.org

--
Boycott the Sugar Bowl! You couldn't pay me to watch that game.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: What would be the best data structure to keep these values

2003-12-16 Thread drieux
On Dec 16, 2003, at 7:13 AM, Hemond, Steve wrote:
[..]
I first trap the results by splitting the results in scalar values 
such as $uid $pid $ppid $stime, etc...

I would like, for each line returned, to hold these values in an 
array, so that, for each uid, I could return its corresponding pid, 
ppid, stime, cmd, etc.
[..]

One strategy would be to go with



from the CPAN.

An alternative strategy would be something like my
pet favorite Process Table Grovellor

In my case I was not trying to use 'ps' as the all
singing all dancing process grabber - and hence went
with the simpler set of arguments to 'ps' that got me
	userNamePID PPIDCOMMAND

This started out from a question much like yours that
got me to a simple piece of code like:


in which you can see that I use a hash of hashes

{
$pid => {
$user => string,
$cmd  => string,
}
}
HTH.

ciao
drieux
---

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: What would be the best data structure to keep these values

2003-12-16 Thread R. Joseph Newton
"Hemond, Steve" wrote:

> Hi people,
>
> I want trap the results of 'ps -ef' command.
>
> I first trap the results by splitting the results in scalar values such as $uid $pid 
> $ppid $stime, etc...
>
> I would like, for each line returned, to hold these values in an array, so that, for 
> each uid, I could return its corresponding pid, ppid, stime, cmd, etc.
>
> What would be the best data structure to use? An hash table in an array? ...

Sound like maybe a hash of hash references.  If you think you might "take it on the 
road", you might make it an anonymous hash, so you can just mainpulate it
by reference:

my $processes = {};
foreach (split /\n/, `ps -ef`) {
  my ($uid $pid $ppid $stime, $etc) = split /\s+/, $_, 5;
  $processes->{$ppid} = {
'uid' => $uid,
'pid' => $pid,
...
  }
}

I am assuming here that the ppid is a sub-process id.  Basically, use any field that 
constitutes a GUID as the key for each child hash.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: What would be the best data structure to keep these values

2003-12-16 Thread drieux
On Dec 16, 2003, at 9:32 AM, R. Joseph Newton wrote:
[..]
I am assuming here that the ppid is a sub-process id.  Basically, use 
any field that constitutes a GUID as the key for each child hash.
[..]

other way around, ppid - parent process id.

Given a Pid the system needs to know whom the
Parent is, so that when the child exits it knows
which process gets the SIG_CHLD.
ciao
drieux
---

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Managing PS data was -Re: What would be the best data structure to keep these values

2003-12-16 Thread drieux


On Dec 16, 2003, at 7:13 AM, Hemond, Steve wrote:
[..]
I first trap the results by splitting the results in scalar values 
such as $uid $pid $ppid $stime, etc...

I would like, for each line returned, to hold these values in an 
array, so that, for each uid, I could return its corresponding pid, 
ppid, stime, cmd, etc.
[..]

There are times when the stench of my hacked code
bothers me, so I have cleaned up a module that I
used in a Project, cleared it with the Owner that
I can take my 'works for hire' "public" and remove
the corporate copyright notice.


Your comments and opinions always appreciated.

ciao
drieux
---

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]