Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

(jeffa) Re: array newbie...(sigh)

by jeffa (Chancellor)
on Mar 15, 2002 at 18:06 UTC ( #152035=note: print w/ replies, xml ) Need Help??


in reply to array newbie...(sigh)

Hmmm ... i _think_ i see what you are trying to do ...

Would this be better for you?

my @occupation = qw(netman jobman mailman batchman writer); foreach my $pation (@occupation) { `ps -ef|grep -i $pation|grep -v grep|wc -l`; }
Each call to ps is exactly the same except who you are grepping for, so you only need to store what is different in your array.

Also, there may be a CPAN module for interacting with ps...

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)


Comment on (jeffa) Re: array newbie...(sigh)
Download Code
Re: (jeffa) Re: array newbie...(sigh)
by ddrumguy (Acolyte) on Mar 15, 2002 at 19:40 UTC
    Well i tried it but it just gives me a list like this: netmanjobmanmailmanbatchmanwriter (acts like the grep isn't even working?) also how do is store the output from the array into a variable ( I need the final number to read: 1113112
    my @arraylist = qw(netman jobman mailman batchman writer); foreach my $process (@arraylist) { `ps -ef|grep -i $process|grep -v grep|wc -l`; } chomp @arraylist; #\s+/ /g for @h; print @arraylist; #@h = ($netman= `ps -ef|grep -i netman|grep -v grep|wc -l`, # $jobman= `ps -ef|grep -i jobman|grep -v grep|wc -l`, # $mailman= `ps -ef|grep -i mailman|grep -v grep|wc -l`, # $batchman= `ps -ef|grep -i batchman|grep -v grep|wc -l`, # $writer= `ps -ef|grep -i writer|grep -v grep|wc -l` # ); #%h = ("a", "$netman", "b", "$jobman", "c", "$mailman", "d", "$batchma +n", "e", "$writer"); #@h = ("$netman","$jobman","$mailman","$batchman","$writer"); #chomp @h; #\s+/ /g for @h; #@h > ;
      Wow. I can honestly say that you have completely lost me! :D

      I recommed you do what PrakashK offered for you. He is absolutely correct in his method, and i think you should really study that. The snippet that i gave you is unfortunately not very practical, i just wanted to demonstrate how to iterate through an array.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: (jeffa) Re: array newbie...(sigh)
by PrakashK (Pilgrim) on Mar 15, 2002 at 20:03 UTC
    foreach my $pation (@occupation) { `ps -ef|grep -i $pation|grep -v grep|wc -l`; }
    Why throw away the results of ps? (What's wrong with using backticks in a void context?)

    If the intention is to count the number of instances of interesting programs from the process list, there is no need to repeat the execution of the ps command.

    my @programs = qw(netman jobman mailman batchman writer); my @all_procs = `ps -ef`; my %counts; for (@programs) { $counts{$_} = scalar grep {/$_/i} @all_procs; } print "$_ $counts{$_}," for keys %counts; print "\n";
    /prakash

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://152035]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-07-11 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (224 votes), past polls