* "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

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to